               (.append sb (char c))
               (recur sb (.read in))))))
 
+(defn- reconnect-if
+  [repl-env]
+  (when-not @(:torn-down repl-env)
+    (@(:reconnect-fn repl-env))))
+
 (defn start-reading-messages
   "Starts a thread reading inbound messages."
   [repl-env opts]
   (.start
-        (Thread.
-          #(try
-            (let [rv (read-messages (:in @(:socket repl-env)) (:response-promise repl-env) opts)]
-              (when (= :eof rv)
-                (close-socket @(:socket repl-env))))
-            (catch IOException e
-              (when-not (.isClosed (:socket @(:socket repl-env)))
-                (.printStackTrace e)))))))
+    (Thread.
+      #(try
+        (let [rv (read-messages (:in @(:socket repl-env)) (:response-promise repl-env) opts)]
+          (when (= :eof rv)
+            (close-socket @(:socket repl-env))
+            (reconnect-if repl-env)))
+        (catch IOException _
+          (reconnect-if repl-env))))))
 
 (defn stack-line->canonical-frame
   "Parses a stack line into a frame representation, returning nil

  (let [{:keys [out]} @(:socket repl-env)
        response-promise (promise)]
    (reset! (:response-promise repl-env) response-promise)
    (write out js)
    (let [response @response-promise]
      (if (= :eof response)
        {:status :error
         :value  "Connection to JavaScriptCore closed."}
        (let [result (json/read-str response
                       :key-fn keyword)]
          (merge
            {:status (keyword (:status result))
             :value  (:value result)}
            (when-let [raw-stacktrace (:stacktrace result)]
              {:stacktrace raw-stacktrace})))))))

(defn load-javascript
  "Load a Closure JavaScript file into the JSC REPL process."

          env (ana/empty-env)
          core (io/resource "cljs/core.cljs")]
      ((println-fn opts) "\nConnecting to" (bonjour-name->display-name bonjour-name) "...\n")
      (reset! (:reconnect-fn repl-env) (partial reconnect repl-env opts))
      (set-up-socket repl-env opts endpoint-address (dec endpoint-port))
      (when (= "true" (:value (jsc-eval repl-env "typeof cljs === 'undefined'")))
        ;; compile cljs.core & its dependencies, goog/base.js must be available
