   #     res.event_json 'HEARTBEAT'
   #
   event_json: (events...,cb) ->
-    @send "event json #{events.join(' ')}", null, cb
+    @send "event json #{events.join(' ')}", cb
 
   # Remove the given event types from the events ACL.
   nixevent: (events...,cb) ->
-    @send "nixevent #{events.join(' ')}", null, cb
+    @send "nixevent #{events.join(' ')}", cb
 
   # Remove all events types.
   noevents: (cb) ->
-    @send "noevents", null, cb
+    @send "noevents", cb
 
   # Generic event filtering
   filter: (header,value,cb) ->
-    @send "filter #{header} #{value}", null, cb
+    @send "filter #{header} #{value}", cb
 
   filter_delete: (header,value,cb) ->
     if value?
-      @send "filter #{header} #{value}", null, cb
+      @send "filter #{header} #{value}", cb
     else
-      @send "filter #{header}", null, cb
+      @send "filter #{header}", cb
 
   # Send an event into the FreeSwitch event queue.
   sendevent: (event_name,args,cb) ->

          for n in ['Content-Type','Reply-Text','Socket-Mode','Control']
            headers[n] = body[n]
            delete body[n]
      when 'text/event-json'
        try
          body = JSON.parse(body)
        catch error
          util.log "JSON #{error} in #{body}"
          return
        event = 'esl_event'
      when 'text/event-plain'
        body = parse_header_text(body)
        event = 'esl_event'
      when 'log/data'
        event = 'esl_log_data'
      when 'text/disconnect-notice'
        event = 'esl_disconnect_notice'
      when 'api/response'
        event = 'esl_api_response'
      else
        event = headers['Content-Type']
    # Build request and response and send them out.
    req = new eslRequest headers,body
    res = new response socket
    if exports.debug
      util.log util.inspect event:event, req:req, res:res
    socket.emit event, req, res
  # Get things started
  socket.emit 'esl_connect', new response socket

#### ESL Server

class eslServer extends net.Server
  constructor: (requestListener,response) ->
    @on 'connection', (socket) ->
      socket.on 'esl_connect', requestListener
      connectionListener socket, response
    super()

# You can use createServer(callback) from your code.
exports.createServer = (requestListener) -> return new eslServer(requestListener)

#### ESL client
class eslClient extends net.Socket
  constructor: (response) ->
    @on 'connect', ->
      connectionListener @, response
    super()

exports.createClient = -> return new eslClient()

#### CallServer: a higher-level interface
#
# This interface is based on my
# [prepaid code](http://stephane.shimaore.net/git/?p=ccnq3.git;a=blob;f=applications/prepaid/)
# and handles the nitty-gritty of setting up the server properly.

exports.createCallServer = (synchronous) ->

    Unique_ID = 'Unique-ID'

    listener = (res) ->
      if exports.debug
        util.log "Incoming connection"
        util.log util.inspect res

      res.connect (req,res) =>

        # Channel data
        channel_data = req.body
        # UUID
        unique_id = channel_data[Unique_ID]

        if exports.debug
          util.log "Incoming call UUID = #{unique_id}"

        # Clean-up at the end of the connection.
        res.on 'esl_disconnect_notice', (req,res) ->
          if exports.debug
            util.log "Received ESL disconnection notice"
          switch req.headers['Content-Disposition']
            when 'linger'      then res.exit()
            when 'disconnect'  then res.end()

        # Use this from your code to force a disconnection.
        #
        #     res.emit 'force_disconnect'
        #
        res.on 'force_disconnect', ->
          util.log 'Hangup call'
          @bgapi "uuid_kill #{unique_id}"

        # Translate channel events into server events.
        res.on 'esl_event', (req,res) ->
          req.channel_data = channel_data
          req.unique_id = unique_id
          server.emit req.body['Event-Name'], req, res

        # Handle the incoming connection
        res.linger (req,res) ->
          res.filter Unique_ID, unique_id, (req,res) ->
            res.event_json 'ALL', (req,res) ->
              req.channel_data = channel_data
              req.unique_id = unique_id
              server.emit 'CONNECT', req, res

    class synchronousResponse extends eslResponse
      intercept_response: (command,args,cb) ->
        @socket.removeAllListeners('esl_command_reply')
        @socket.removeAllListeners('esl_api_response')
        server.removeAllListeners('CHANNEL_EXECUTE_COMPLETE')
        if command is 'sendmsg' and args['call-command'] is 'execute'
          server.on 'CHANNEL_EXECUTE_COMPLETE', cb
        else
          # Make sure we are the only one receiving command replies
          # Register the callback for the proper event types.
          @socket.on 'esl_command_reply', cb
          @socket.on 'esl_api_response', cb

    response = null
    if synchronous
      response = synchronousResponse

    server = new eslServer listener, response

#
# If you'd like to get realtime channel variables and synchronous commands, do
#
#     server = esl.createCallServer()
#
#     server.on 'CONNECT', (req,res) ->
#       # "verbose_events" will send us channel data after each "command".
#       res.command 'verbose_events', null, (req,res) ->
#         # You may now access realtime variables from req.body
#         foo = req.body.variable_foo
#         res.command 'play-file', 'voicemail/vm-hello', (req,res) ->
#           # Wait for the command to finish.
#           # The application continues here
#
#     server.listen 7000
#
# The asynchronous version of "command" is "execute".
#
# An asynchronous server will look this way:
#
#     server = esl.createCallServer()
#
#     server.on 'CONNECT', (req,res) ->
#       uri = req.body.variable_sip_req_uri
#
#     # Other FreeSwitch channel events are available as well:
#     server.on 'CHANNEL_ANSWER', (req,res) ->
