 
 (defn connect-to-peer [state peer]
   (let [socket @(client "localhost" (+ 8080 peer))]
-    (warn "client connected")
-    (utils/dbg s/put! socket (:id @state))
-    (warn "after put")
-    (swap! state assoc-in [:peer-sockets peer] socket)))
+    (d/let-flow [_ (s/put! socket (:id @state))
+                 resp (s/take! socket)]
+                (swap! state assoc-in [:peer-sockets peer] socket))))
 
 (defn send-pkt [state pkt]
   (warn "sending from: " (:id @state) " to: " (:dst pkt) " of type: " (:type pkt))
   (let [peer (:dst pkt)]
     (when-not (get-in @state [:peer-sockets peer])
-      (warn "connecting to peer " peer)
-      (connect-to-peer state peer))
+      (info "connecting...")
+      @(connect-to-peer state peer))
     (s/put! (get-in @state [:peer-sockets peer]) pkt))
   true)
 

  true)

(defn handle-rx-pkt [state dt pkt]
  (warn "handle-rx-pkt")
  (swap! state (fn [s]
                 (-> s
                     (update-in [:rx-queue] conj pkt)

(defn incoming-message-loop
  [f state]
  (fn [s info]
    (d/let-flow [handshake (s/take! s)
                 _ (s/put! s (:id @state))]
                (if (get-in @state [:peer-sockets handshake])
                  (warn (:id @state )": already connected to: "  handshake)
                  (swap! state assoc-in [:peer-sockets handshake] s))
                (d/loop []
                  (warn "loop..." (:id @state))
                  (-> (s/take! s ::none)
                      (d/chain
                       (fn [msg]
                         (if (= ::none msg)
                           (do
                             (warn "closing!")
                             (s/close! s))
                           (do
                             (f msg)
                             (d/recur)))))
                      (d/catch
                          (fn [ex]
                            (warn ex "in close")
                            (s/put! s (str "CLOSED:" ex))
                            (s/close! s))))))))

(defn now []
  (System/currentTimeMillis))
