             @currentSearches[i].callback json
             @currentSearches.splice i, 1
             break
-      else
-        if json['command']
-          #console.log('Command: ', json)
-        else if typeof(json['msgid']) == 'number'
-          if not json['success']
-            1 == 1
-            #console.log(json)
+    @emit command, json
+
+
+  treatPacket: (packet) ->
+    json = @extractPacketJson(packet)
+    @executeCallback(json)
+    @treatCommand(json)
+
+
+  onMessage: (msg) ->
+    data = msg.data
+    return @treatHeartbeat(data) if @isHeartbeat(data)
+    @log "> #{data}"
+    return @treatNoSession(data) if @isNoSession(data)
+    @lastActivity = Date.now()
+    @treatPacket(data)
 
 
   _heartbeat: (msg) ->

    #console.log 'THIS IS WEIRD AND SHOULD NOT APPEAR.'


  onMessage: (msg) ->
    data = msg.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()

    len_rgx = /~m~([0-9]+)~m~/
    len = data.match(len_rgx)[1]
    json = JSON.parse(data.substr(data.indexOf('{'), len))
    index = 0
    while index < @_cmds.length
      [id, rq, clb] = @_cmds[index]

  log: (message) ->
    if @debug
      if @stdout == 'stderr'
        console.error message
      else
        console.log message


  connect: (roomId) ->
