         (.flush writer)
         (.close conn))))
 
-(defn return [message]
-  (println message))
+(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 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 process-form []
+  (when-let [first-form (ffirst @pending-forms)]
+    (println "first-form: " first-form)
+    (send-for-eval first-form)))
 
-(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))))))))
+(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

           java.io.InputStreamReader
           java.io.OutputStreamWriter
           java.net.Socket
           java.net.ServerSocket))

(defonce server-state (atom {:socket nil
                             :port nil
                             :connection nil
                             :next-message nil}))

(defn send-and-close
  "Send this message on the connection and close it."
  [conn message]
  (with-open [writer (BufferedWriter.
                      (OutputStreamWriter. (.getOutputStream conn)))]
    (do (.write writer message)

  []
  (.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