                   EpollServerSocketChannel
                   NioServerSocketChannel)
         channel-group (DefaultChannelGroup. (str "tcp-server " host ":" port)
-                                            (ImmediateEventExecutor/INSTANCE))
+                        (ImmediateEventExecutor/INSTANCE))
         initializer (channel-initializer-done 
-                      (gen-tcp-handler channel-group 
-                                       (create-server-handler buf-recvd-ch)))] 
-    ; Configure bootstrap
+                     (gen-tcp-handler channel-group 
+                                      (create-server-handler buf-recvd-ch)))] 
+                                        ; Configure bootstrap
     (doto bootstrap
       (.group boss-group worker-group)
       (.channel channel)

  [config peer-site peer-sites]
  (let [used-ports (->> (vals peer-sites) 
                        (filter 
                          (fn [s]
                            (= (:netty/external-addr peer-site) 
                               (:netty/external-addr s))))
                        (map :netty/port)
                        set)
        port (first (remove used-ports

        (.channel ^ChannelFuture (.connect b host port))))))

(defn app [daemon parsed-ch inbound-ch release-ch retry-ch]
  (go-loop []
           (try (let [msg (<!! parsed-ch)
                      t ^byte (:type msg)]
                  (cond (= t protocol/messages-type-id) 
                        (doseq [message (:messages msg)]
                          (>!! inbound-ch message))

                        (= t protocol/ack-type-id)
                        (acker/ack-message daemon
                                           (:id msg)
                                           (:completion-id msg)
                                           (:ack-val msg))

                        (= t protocol/completion-type-id)
                        (>!! release-ch (:id msg))

                        (= t protocol/retry-type-id)
                        (>!! retry-ch (:id msg))

                        :else
                        (throw (ex-info "Unexpected message received from Netty" {:message msg}))))
                (catch Exception e
                  (taoensso.timbre/error e)
                  (throw e)))
           (recur)))

(defn buf-recv-loop [messenger buf-recv-ch parsed-ch]
  (go-loop []
           (let [buf (<!! buf-recv-ch)]
             (try 
               (let [decompressed (protocol/read-buf (:decompress-f messenger) buf)]
                 (>!! parsed-ch decompressed)
                 ;; undo retain added in server handler
                 (.release ^ByteBuf buf))
               (catch Throwable t
                 (timbre/fatal t "Exception in recv loop.")))
             (recur))))

(defrecord NettyTcpSockets [peer-group]
  component/Lifecycle
