           @sock = new WebSocket(url, headers, timeout)
 
           # The WebSocket was closed
-          @sock.once 'close', =>
+          @sock.once 'close', (code, cause) =>
+            # WebSocket status codes for socket close
+            #  1000  CLOSE_NORMAL
+            #  1001  CLOSE_GOING_AWAY
+            #  1002  CLOSE_PROTOCOL_ERROR
+            #  1003  CLOSE_UNSUPPORTED
+            #  1005  CLOSE_NO_STATUS
+            #  1006  CLOSE_ABNORMAL
+            #  1007  Unsupported Data
+            #  1008  Policy Violation
+            #  1009  CLOSE_TOO_LARGE
+            #  1010  Missing Extension
+            #  1011  Internal Error
+            #  1012  Service Restart
+            #  1013  Try Again Later
+            #  1015  TLS Handshake
+            #
+            #  https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
+
             if @autoReconnect == true
               @sock = null
 


    .then (response) -> response.channels

  # Unsubscribe from a channel.
  unsubscribeAll: ->
    @handlers = {}

    new P (resolve, reject) =>
      if @sock?
        seq = @sequence
        @sequence += 1
        
        record =
          seq: seq
          action: 'unsubscribe-all'
        
        @sock.send JSON.stringify(record)
        .then =>
          @outstanding.set seq,
            resolve: resolve
            reject: reject
        .catch (error) ->
          message = "Socket error while unsubscribing from channel:"
          logger.error message, error
          reject new errors.PubSubError message, error 
        
      else
        message = "Could not unsubscribe from channel as the socket is currently disconnected."
        logger.warn message
        reject new errors.PubSubError message, null

    .then (response) -> response.channels

  # List all channels to which this connection is subscribed.
  listSubscriptions: ->
    new P (resolve, reject) =>
      if @sock?
        seq = @sequence
        @sequence += 1
        
        record =
          seq: seq

            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 += 1
        
        record =
          seq: seq
          action: 'unsubscribe'
          channel: channel
        
        @sock.send JSON.stringify(record)
