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


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

exports.createClient = -> return new eslClient()


#### CallServer Example
#
# From the dialplan, use
#    <action application="socket" data="127.0.0.1:7000 async full"/>
# to hand the call over to an ESL server.
#
# If you'd like to get realtime channel variables and synchronous commands, do
#
#     server = esl.createCallServer true
#
#     server.on 'CONNECT', (req,res) ->
#       res.execute 'verbose_events', null, (req,res) ->
#         # You may now access realtime variables as
#         foo = req.body.variable_foo
#         res.execute 'play-file', 'voicemail/vm-hello', (req,res) ->
#           # This call with wait for the command to finish
#           # Your application continues here
#
#     server.listen 7000
#
# Note that although the socket command was called (in the dialplan above) with
# the "async" parameter, createCallServer is processing the commands synchronously.
# Of course you can work synchronously without using verbose_events; you may still
# obtain realtime values by using the API (lookup the "eval" command).
# However you cannot use verbose_events asynchronously, unless you capture the
# CHANNEL_EXECUTE_COMPLETE event.
#
# An asynchronous server will look this way:
#
#     server = esl.createCallServer()
#
#     server.on 'CONNECT', (req,res) ->
