   []
   (.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
;;
;; userid=joe&password=guessme

#_(defn read-post [line reader]
  (loop [next-line (.readLine reader)
         content-length 0
         request [line]]
    (if next-line
      (recur (.readLine reader) (conj request next-line))
      request)))

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

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

(defn- server-loop
  [server-socket]

        (.flush writer)
        (.close conn))))

(defn send-for-eval
  "If a connection is available then send this form."
  [message]
  (when-let [conn (:connection @server-state)]
    (println "sending for eval")
    (send-and-close conn message)))

(defn process-form []
  (when-let [first-form (ffirst @pending-forms)]
    (println "first-form: " first-form)
    (send-for-eval first-form)))

(defn return [return-value]
  (let [[[_ first-promise]] @pending-forms]
    (swap! pending-forms drop 1)
    (deliver first-promise return-value)
    (process-form)))

(defn send-forms [forms]
  (let [pending (reset! pending-forms
                        (map #(vector % (promise)) forms))]
    (println "pending forms: " pending)
    (process-form)
    ;; TODO: (map second pending) should be consumed by a real REPL
    (doseq [return-value (map second pending)]
      (println @return-value))))

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