     for opt in options
       query.push "#{opt}=#{encodeURIComponent(options[opt])}"
 
-    self = @
     httpOptions =
       host: 'turntable.fm'
       port: 80
       path: '/api/room.directory_rooms?' + query.join("&")
-    http.get httpOptions, (res) ->
+    http.get httpOptions, (res) =>
       dataStr = ''
-      res.on 'data', (chunk) ->
+      res.on 'data', (chunk) =>
         dataStr += chunk.toString()
-      res.on 'end', ->
+      res.on 'end', =>
         try
           data = JSON.parse dataStr
         catch err
           data = []
-        callback.call self, data
+        callback.call @, data
 
 
   stalk: ->
-    self     = @
     userId   = ''
     allInfos = false
     callback = ->



  whichServer: (roomid, callback) ->
    self = @
    options =
      host: 'turntable.fm'
      port: 80
      path: "/api/room.which_chatserver?roomid=#{roomid}"
    http.get options, (res) ->
      dataStr = ''
      res.on 'data', (chunk) ->
        dataStr += chunk.toString()
      res.on 'end', ->
        try
          data = JSON.parse dataStr
        catch err
          data = []
        if data[0]
          [host, port] = data[1].chatserver
          callback.call(self, host, port)
        else
          self.log "Failed to determine which server to use: #{dataStr}"


  setTmpSong: (data) ->



  listen: (port, address) ->
    http.createServer((req, res) =>
      dataStr = ''
      req.on 'data', (chunk) =>
        dataStr += chunk.toString()
      req.on 'end', =>
        data = querystring.parse(dataStr)
        req._POST = data
        @emit 'httpRequest', req, res
    ).listen(port, address)


  tcpListen: (port, address) ->
    net.createServer((socket) =>
      socket.on 'connect', =>
        @emit 'tcpConnect', socket
      socket.on 'data', (data) =>
        msg = data.toString()
        if msg[msg.length - 1] == '\n'
          @emit 'tcpMessage', socket, msg.substr(0, msg.length-1)
      socket.on 'end', ->
        @emit 'tcpEnd', socket
    ).listen(port, address)


