           (.removeServiceListener mdns-service reg-type service-listener)
           (.close mdns-service))))))
 
-(defn resolve-bonjour [bonjour-name timeout]
-  (let [reg-type "_http._tcp.local."
-        mdns-service (JmDNS/create)]
-    (try
-      (let [info (.getServiceInfo mdns-service reg-type bonjour-name timeout)]
-        (when info
-          {:address (.getAddress info)
-           :port    (.getPort info)}))
-      (finally
-        (future
-          (.close mdns-service))))))
-
 (defn socket [host port]
   (let [socket (doto (Socket. host port) (.setKeepAlive true))
         in     (io/reader socket)

    (socket address port))
  ;; Start dedicated thread to read messages from socket
  (start-reading-messages repl-env opts))

(defn reconnect [repl-env opts]
  ((print-fn opts) "Reconnecting ...")
  (let [bonjour-name @(:bonjour-name repl-env)
        endpoint (resolve-bonjour bonjour-name 15000)]
    (if endpoint
      (let [endpoint-address (.getHostAddress (:address endpoint))
            endpoint-port (:port endpoint)]
        (mount-webdav repl-env bonjour-name endpoint-address endpoint-port)
        (set-up-socket repl-env opts endpoint-address (dec endpoint-port)))
      ((print-fn opts) "Failed to reconnect."))))

(defn setup
  [repl-env opts]

  (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."
