   (.start
         (Thread.
           #(try
-            (let [rv (read-messages (:in @(:socket repl-env)) (:response-channel repl-env))]
+            (let [rv (read-messages (:in @(:socket repl-env)) (:response-promise repl-env))]
               (when (= :eof rv)
                 (close-socket @(:socket repl-env))))
             (catch IOException e

  [repl-env js]
  (let [{:keys [out]} @(:socket repl-env)
        response-promise (promise)]
    (reset! (:response-channel repl-env) response-promise)
    (write out js)
    (let [response @response-promise]
      (if (= :eof response)

  (.write out (int 0)) ;; terminator
  (.flush out))

(defn read-messages [^BufferedReader in response-promise]
  (loop [sb (StringBuilder.) c (.read in)]
    (cond
      (= c -1) (do
                 (if-let [resp-promise @response-promise]
                   (deliver resp-promise :eof))
                 :eof)
      (= c 1) (do
                (print (str sb))
                (flush)
                (recur (StringBuilder.) (.read in)))
      (= c 0) (do
                (deliver @response-promise (str sb))
                (recur (StringBuilder.) (.read in)))
      :else (do
              (.append sb (char c))
