   (:import [java.net Socket]
            [java.io OutputStreamWriter InputStreamReader File]))
 
-(defn repl-server [project port]
+(defn repl-server [project host port]
   (let [init-form [:init `#(let [is# ~(:repl-init-script project)
                                  mn# '~(:main project)]
                              (when (and is# (.exists (File. str)))

                    [java.io ~'InputStreamReader ~'OutputStream
                     ~'OutputStreamWriter ~'PrintWriter]
                    [clojure.lang ~'LineNumberingPushbackReader]))
         (let [server# (ServerSocket. ~port)
               socket# (.accept server#)
               ins# (.getInputStream socket#)
               outs# (.getOutputStream socket#)]
           (binding [*in* (-> ins# InputStreamReader.
                              LineNumberingPushbackReader.)
                     *out* (OutputStreamWriter. outs#)
                     *err* (PrintWriter. outs# true)]
             (clojure.main/repl ~@init-form))
           (.close server#)))))

(defn copy-out [reader]
  (Thread/sleep 100)

(defn repl
  "Start a repl session for the current project."
  [project]
  (let [host (or (System/getenv "LEIN_REPL_HOST") "localhost")
        port (Integer. (or (System/getenv "LEIN_REPL_PORT")
                           (dec (+ 1024 (rand-int 64512)))))
        server-form (repl-server project host port)
        server-thread (Thread. #(try (eval-in-project project server-form)
                                     (catch Exception _)))]
    (.start server-thread)
