 
         @write finalArray.buffer
 
+    ## Emulate the event emitter interface
+
+    addListener: (event, listener) ->
+        unless @_events[event]?
+            @_events[event] = []
+        @_events[event].push(listener)
+
+    on: (event, listener) -> @addListener(event, listener)
+
+    once: (event, listener) ->
+        listener._once = true
+        addListener(event, listener)
+
+    removeListener: (event, listener) ->
+        if @_events[event]?
+            @_events[event] = @_events[event].filter (lst) -> (lst isnt listener)
+
+    removeAllListeners: (event) -> delete @_events[event]
+
+    listeners: (event) -> @_events[event]
+
+    emit: (event, args...) ->
+        toRemove = []
+        for lst in @_events[event]
+            lst.apply(null, args)
+            if lst._once
+                toRemove.push(lst)
+        for lst in toRemove
+            @removeListener(event, lst)
+
 class TcpConnection extends Connection
     @isAvailable: -> typeof require isnt 'undefined' and require('net')
 

                if xhr.status is 200
                    @_url = url
                    @_connId = (new DataView xhr.response).getInt32(0, true)
                    @_connect()
                else
                    @_error()
        xhr.send()

    cancel: ->

    constructor: (embeddedServer, callback) ->
        super({}, callback)
        @_embeddedServer = embeddedServer
        @emit 'connect'

    write: (chunk) -> @_data(@_embeddedServer.execute(chunk))

