       @connection.send JSON.stringify { action: 'unsubscribe', identifier: identifier }
 
   pingReceived: (timestamp) =>
-    if @lastPingTime? and (timestamp - @lastPingTime) > @MAX_PING_INTERVAL
+    if @lastPingTime? and (timestamp - @lastPingTime) > @PING_STALE_INTERVAL
       console.log "Websocket connection is stale. Reconnecting.."
-      @connection.close()
+      @connection?.close()
     else
+      @startWaitingForPing()
       @lastPingTime = timestamp

      @subscribers[data.identifier]?.onReceiveData(data.message)

  connected: =>
    @resetConnectionAttemptsCount()

    for identifier, callbacks of @subscribers

    interval = (Math.pow(2, @connectionAttempts) - 1) * 1000
    if interval > @MAX_CONNECTION_INTERVAL then @MAX_CONNECTION_INTERVAL else interval

  startWaitingForPing: =>
    @clearPingWaitTimeout()

    @waitForPingTimeout = setTimeout =>
      console.log "Ping took too long to arrive. Reconnecting.."
      @connection?.close()
    , @PING_STALE_INTERVAL * 1000

  clearPingWaitTimeout: =>
    clearTimeout(@waitForPingTimeout)

  resetPingTime: =>
    @lastPingTime = null

  disconnected: =>
