             resolve: resolve
             reject: reject
         .catch (error) ->
-          message = "Socket error while publishing message:"
-          logger.error message, error
-          reject new errors.PubSubError message, error 
-        
-      else
-        message = "Could not publish a message as the socket is currently disconnected."
-        logger.warn message
-        reject new errors.PubSubError message, null
-
-  # Subscribe to a channel.
-  subscribe: (channel, handler) ->
-    if typeof handler == 'function'
-      @handlers[channel] = handler
-
-    new P (resolve, reject) =>
-      if @sock?
-        seq = @sequence
-        @sequence += 1
-        
-        record =
-          seq: seq
-          action: 'subscribe'
+          message = "Socket error while subscribing to channel:"
+          logger.error message, error
+          reject new errors.PubSubError message, error 
+        
+      else
+        message = "Could not subscribe to channel as the socket is currently disconnected."
+        logger.warn message
+        reject new errors.PubSubError message, null
+
+    .then (response) -> response.channels
+
+    .catch (error) =>
+      logger.error "Error subscribing to channel '#{channel}'", error
+      delete @handlers[channel]
+      throw error
+
+  # Unsubscribe from a channel.
+  unsubscribe: (channel) ->
+    delete @handlers[channel]
+
+    new P (resolve, reject) =>
+      if @sock?
+        seq = @sequence += 1
+        
+        record =
+          seq: seq
+          action: 'unsubscribe'
           channel: channel
         
         @sock.send JSON.stringify(record)

            resolve: resolve
            reject: reject
        .catch (error) ->
          message = "Socket error while subscribing to channel:"
          logger.error message, error
          reject new errors.PubSubError message, error 
        
      else
        message = "Could not subscribe to channel as the socket is currently disconnected."
        logger.warn message
        reject new errors.PubSubError message, null

    .then (response) -> response.channels

    .catch (error) =>
      logger.error "Error subscribing to channel '#{channel}'", error
      delete @handlers[channel]
      throw error

  # Unsubscribe from a channel.
  unsubscribe: (channel) ->
    delete @handlers[channel]

    new P (resolve, reject) =>
      if @sock?
        seq = @sequence
        @sequence += 1
        
        record =
          seq: seq
          action: 'unsubscribe'
          channel: channel
        
        @sock.send JSON.stringify(record)
        .then =>

        reject new errors.PubSubError message, null

    .then (response) -> response.uuid

  # Publish a message to a channel expecting an acknowledgement on success
  publishWithAck: (channel, message) ->
    @publish channel, message, true
    .then (seq) =>
      @outstanding.set seq,
        resolve: resolve
        reject: reject
    
  # Publish a message to a channel.
  publish: (channel, message, ack) ->
    new P (resolve, reject) =>
      if @sock?
        ack = undefined if ack != true
        seq = @sequence += 1
        
        record =
          seq: seq
          action: 'pub'
          chan: channel
          msg: message
          ack: ack
        
        @sock.send JSON.stringify(record)
        .then => resolve seq
        .catch (error) ->
          message = "Socket error while publishing message:"
          logger.error message, error
          reject new errors.PubSubError message, error 
        
      else
        message = "Could not publish a message as the socket is disconnected."
        logger.warn message
        reject new errors.PubSubError message, null

  # Subscribe to a channel.
  subscribe: (channel, handler) ->
    if typeof handler == 'function'
      @handlers[channel] = handler

    new P (resolve, reject) =>
      if @sock?
        seq = @sequence += 1
        
        record =
          seq: seq
          action: 'subscribe'
          channel: channel
        
        @sock.send JSON.stringify(record)
        .then =>
