             @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) ->

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

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


  connect: (roomId) ->
