     #console.log 'THIS IS WEIRD AND SHOULD NOT APPEAR.'
 
 
-  onMessage: (msg) ->
-    data = msg.data
-
+  isHeartbeat: (data) ->
     heartbeat_rgx = /~m~[0-9]+~m~(~h~[0-9]+)/
-    if data.match heartbeat_rgx
-      @_heartbeat data.match(heartbeat_rgx)[1]
-      @lastHeartbeat = Date.now()
-      @updatePresence()
-      return
-
-    @log "> #{data}"
-
-    if msg.data == '~m~10~m~no_session'
-      @userAuthenticate ->
-        if not @_isConnected
-          @getFanOf (data) ->
-            @fanOf = data.fanof
-            @updatePresence()
-            # TODO: I don't like setInterval !
-            setInterval(@updatePresence.bind(@), 10000)
-            @emit 'ready'
-        @callback()
-        @_isConnected = true
-      return
-
-    @lastActivity = Date.now()
-
+    data.match heartbeat_rgx
+
+
+  isNoSession: (data) ->
+    data == '~m~10~m~no_session'
+
+
+  treatHeartbeat: (packet) ->
+    heartbeat_rgx = /~m~[0-9]+~m~(~h~[0-9]+)/
+    @_heartbeat packet.match(heartbeat_rgx)[1]
+    @lastHeartbeat = Date.now()
+    @updatePresence()
+
+
+  treatNoSession: (packet) ->
+    @userAuthenticate ->
+      if not @_isConnected
+        @getFanOf (data) ->
+          @fanOf = data.fanof
+          @updatePresence()
+          # TODO: I don't like setInterval !
+          setInterval(@updatePresence.bind(@), 10000)
+          @emit 'ready'
+      @callback()
+      @_isConnected = true
+
+
+  extractPacketJson: (packet) ->
     len_rgx = /~m~([0-9]+)~m~/
-    len = data.match(len_rgx)[1]
-    json = JSON.parse(data.substr(data.indexOf('{'), len))
+    len = packet.match(len_rgx)[1]
+    try
+      return JSON.parse(packet.substr(packet.indexOf('{'), len))
+    catch err
+      return null
+
+
+  executeCallback: (json) ->
     index = 0
     while index < @_cmds.length
       [id, rq, clb] = @_cmds[index]

  log: (message) ->
    if @debug
      if @stdout == 'stderr'
        console.error "> #{data}"
      else
        console.log "> #{data}"


  connect: (roomId) ->

            if json.success == true
              currentDj   = json.room.metadata.current_dj
              currentSong = json.room.metadata.current_song
              @currentDjId = currentDj if currentDj
              @currentSongId = currentSong._id if currentSong
          when 'room.register'
            if json.success == true
              @roomId = rq.roomid
