       # Make sure we are the only one receiving command replies
       @socket.removeAllListeners('esl_command_reply')
       if cb?
-        @on 'esl_command_reply', (req,res) ->
+        @socket.on 'esl_command_reply', (req,res) ->
           cb(req,res)
 
       @socket.write "#{command}\n"

exports.createServer = (requestListener) -> return new eslServer(requestListener)

class eslClient extends net.Socket
  constructor: (host,port) ->
    @on 'connect', connectionListener
    super port, host

exports.createClient = (host,port) -> return new eslClient(host,port)


# Examples:
###

client =  createClient(host,port)
client.on 'esl_auth_request', (req,res) ->
    res.send "auth #{auth}", () ->
      res.send 'event json HEARTBEAT'

###


    @send "api #{command}", null, cb

  bgapi: (command,cb) ->
    @send "bgapi #{command}", null, (req,res) ->
      if cb?
        r = res.header['Reply-Text']?.match /\+OK Job-UUID: (.+)$/
        cb r[1]
