 class eslResponse
   constructor: (@socket) ->
 
+  # If a callback is provided we attempt to trigger it
+  # when the operation completes.
+  # By default, the trigger is done as soon as the ESL command
+  # finishes.
+  intercept_response: (command,args,cb) ->
+    # Make sure we are the only one receiving command replies
+    @socket.removeAllListeners('esl_command_reply')
+    @socket.removeAllListeners('esl_api_response')
+    # Register the callback for the proper event types.
+    @socket.on 'esl_command_reply', cb
+    @socket.on 'esl_api_response', cb
+
   # A generic way of sending commands back to FreeSwitch.
   #
   #      send (string,hash,function(req,res))

              req.channel_data = channel_data
              req.unique_id = unique_id
              server.emit 'CONNECT', req, res

        event = headers['Content-Type']
    # Build request and response and send them out.
    req = new eslRequest headers,body
    res = new_response()
    if intercept_response?
      res.intercept_response = intercept_response
    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()

#### ESL Server

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

# You can use createServer(callback) from your code.
