 
            (def state (atom {}))
            (def storage (storage-factory/createHTML5SessionStorage "figwheel.repl"))
+           (defn session-name []
+             (.get storage (str ::session-name)))
+
+           (defn response-for [{:keys [uuid]} response-body]
+             {:uuid uuid
+              :session-name (session-name)
+              :response response-body})
+
+           (defn respond-to [{:keys [websocket] :as old-msg} response-body]
+             (let [response (response-for old-msg response-body)]
+               (.send websocket (pr-str response))))
 
            (defmulti message :op)
            (defmethod message "naming" [msg]
              (.set storage (str ::session-name) (:session-name msg)))
 
+           (def ^:dynamic *eval-js* (fn [code] (js* "eval(~{code})")))
+
+           (let [ua-product-fn
+                 #(cond
+                    (not (nil? goog/nodeGlobalRequire)) :chrome
+                    product/SAFARI    :safari
+                    product/CHROME    :chrome
+                    product/FIREFOX   :firefox
+                    product/IE        :ie)]
+             (defn eval-javascript** [code]
+               (let [ua-product (ua-product-fn)]
+                 (try
+                   (let [sb (js/goog.string.StringBuffer.)]
+                     (binding [cljs.core/*print-newline* false
+                               cljs.core/*print-fn* (fn [x] (.append sb x))]
+                       (let [result-value (*eval-js* code)]
+                         {:status :success
+                          :out (str sb)
+                          :ua-product ua-product
+                          :value result-value})))
+                   (catch js/Error e
+                     {:status :exception
+                      :value (pr-str e)
+                      :ua-product ua-product
+                      :stacktrace (.-stack e)})
+                   (catch :default e
+                     {:status :exception
+                      :ua-product ua-product
+                      :value (pr-str e)
+                      :stacktrace "No stacktrace available."})))))
+
+           (defmethod message "eval" [{:keys [code] :as msg}]
+             (let [result (eval-javascript** code)]
+               (respond-to msg result)))
+
            (defn connect [websocket-url]
-             (doto (goog.net.WebSocket.)
-               (.addEventListener goog.net.WebSocket.EventType.MESSAGE
-                                  (fn [e]
-                                    (when-let [msg (gobj/get e "message")]
-                                      (try
-                                        (prn msg)
-                                        (message (js->clj (js/JSON.parse msg) :keywordize-keys true))
-                                        (catch js/Error e
-                                          (js/console.error e))))))
-               (.addEventListener goog.net.WebSocket.EventType.OPENED
-                                  (fn [e]
-                                    (js/console.log "OPENED")
-                                    (js/console.log e)))
-               (.open websocket-url)
-               ))
+             (let [websocket (goog.net.WebSocket.)]
+               (doto websocket
+                 (.addEventListener goog.net.WebSocket.EventType.MESSAGE
+                                    (fn [e]
+                                      (when-let [msg (gobj/get e "message")]
+                                        (try
+                                          (prn msg)
+                                          (js/console.log msg)
+                                          (message (assoc
+                                                    (js->clj (js/JSON.parse msg) :keywordize-keys true)
+                                                    :websocket websocket))
+                                          (catch js/Error e
+                                            (js/console.error e))))))
+                 (.addEventListener goog.net.WebSocket.EventType.OPENED
+                                    (fn [e]
+                                      (js/console.log "OPENED")
+                                      (js/console.log e)))
+                 (.open websocket-url)
+                 )))
            (js/console.log (.get storage (str ::session-name)))
            (js/console.log (nil? (.get storage (str ::session-name))))
            (connect (str "ws://localhost:9500/fargot" (when-let [n (.get storage (str ::session-name))]


(let [timeout-val (Object.)]
  (defn evaluate [{:keys [focus-session-name ;; just here for consideration
                          last-session-name
                          repl-timeout
                          broadcast] :as repl-env} js]
    ;; TODO if focus session is set
    ;; if it is available send eval to it otherwise
    ;; choose next best session and send message to it
    ;; and the set the focus session to the new session name
    (wait-for-connection)
    (let [connections (connections-available repl-env)
          youngest-connection (first connections)
          result (let [v (deref (send-for-eval (if broadcast

                                               js)
                                (or repl-timeout 8000)
                                timeout-val)]
                   (if (= timeout-val v)
                     {:status :exception
                      :value "Eval timed out!"
