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

#### Connection Listener (socket events handler)
# This is modelled after Node.js' http.js

connectionListener= (socket) ->
  socket.setEncoding('ascii')
  parser = new eslParser socket
  socket.on 'data', (data) ->  parser.on_data(data)

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

    server = new eslServer listener, intercept_response
