   (doto 
     (->ConnectionManager messenger 
                          site
-                         (->ConnectionState (atom :initializing)) 
+                         (atom :initializing) 
                          (atom (make-pending-chan messenger))
                          (atom nil))
     connect))

  (connected [this]))

(defn add-failed-check 
  "Check if the future failed. link-val is used as a staleness check
  for the connection as it should not be reset if it has already been reset."
  [f connection buf]
  (.addListener f (reify GenericFutureListener
                    (operationComplete [_ _]

                        (timbre/error "Message failed to send: " (.cause f))
                        (reset-connection connection))))))

(defn make-pending-chan [messenger]
  (chan (sliding-buffer (:pending-buffer-size messenger))))

(defn state->connecting [state]
  (compare-and-set! state :initializing :connecting))

(defn state->reset [state] 
    (compare-and-set! state :connected :reset))

(defn state->connecting [state]
  (or (compare-and-set! state :reset :connecting)
      (compare-and-set! state :initializing :connecting)
      (compare-and-set! state :failed :connecting)))

(defn state->failed [state]
  (compare-and-set! state :connecting :failed))

(defn state->connected [state]
  (compare-and-set! state :connecting :connected))

(defrecord ConnectionManager [messenger site state pending-ch channel]
  IConnectionManager
  (reset-connection [connection]
    (when (state->reset state)
      (reset! pending-ch (make-pending-chan messenger))
      (reset! channel nil)
      (connect connection)))

  (enqueue-pending [connection buf]
    (case @state
      :initializing (>!! @pending-ch buf)
      :connecting (>!! @pending-ch buf)
      :reset (connect connection)
