   []
   (.close (:socket @server-state)))
 
+;;; (in-ns 'cljs.browser-repl)
+;;; (start-server 9000)
+;;; curl -v -d "ready" http://127.0.0.1:9000
+;;; (send-forms ["test"])
+;;; curl -v -d "test-result" http://127.0.0.1:9000
        (.flush writer)
        (.close conn))))

(defn return [message]
  (println message))

(defn evaluate
  "If a connection is available then send this form, if not then
  put the message in the next-message slot."
  [message]
  (if-let [conn (:connection @server-state)]
    (send-and-close conn message)
    (swap! server-state (fn [old] (assoc old :next-message message)))))

(defn send-message
  "Check to see if there is a message to send and if there is send
  it."
  []
  (let [conn (:connection @server-state)]
    (when (and conn (not (.isClosed conn)))
      (when-let [message (:next-message @server-state)]
        (do (send-and-close conn message)
            (swap! server-state (fn [old] (assoc old :next-message nil))))))))

;; POST /login.jsp HTTP/1.1
;; Host: www.mysite.com

;;
;; userid=joe&password=guessme

(comment

  (parse-headers
   ["Host: www.mysite.com"
    "User-Agent: Mozilla/4.0"
    "Content-Length: 27"
    "Content-Type: application/x-www-form-urlencoded"])
)

(defn parse-headers [header-lines]
  (apply hash-map
   (mapcat
    (fn [line]
      (let [[k v] (str/split line #":" 2)]
        [(keyword (str/lower-case k)) (str/triml v)]))
    header-lines)))

;;; assumes first line already consumed
(defn read-headers [rdr]
  (loop [next-line (.readLine rdr)
         header-lines []]
    (if (= "" next-line)
      header-lines                      ;we're done reading headers
      (recur (.readLine rdr) (conj header-lines next-line)))))

(defn read-post [rdr]
  (let [headers (parse-headers (read-headers rdr))
        content-length (Integer/parseInt (:content-length headers))
        content (char-array content-length)]
    (io! (.read rdr content 0 content-length)
         (String. content))))

(defn read-request [reader]
  (let [line (.readLine reader)]
    (if (.startsWith line "POST")
      (read-post reader)
      line)))

(defn- handle-connection
  [conn]
  (let [reader (BufferedReader. (InputStreamReader. (.getInputStream conn)))]
    (if-let [message (read-request reader)]
      (do (when (not= message "ready")
            (return message))
          (swap! server-state (fn [old] (assoc old :connection conn)))))))

(defn- server-loop
  [server-socket]
