     
     @openDeferred.promise()
 
+
   close : ->
     if @socket
       @socket.close()
       @socket = null
       @openDeferred = null
 
+
   send : (data) ->
 
     @initialize().pipe =>
     
+      { transmitBuffer, socketHandle } = @createPackage(data)
+
       deferred = $.Deferred()
-      { transmitBuffer, socketHandle } = @createPackage(data)
-      socket = @socket
-
-      detachHandlers = ->
-        socket.removeEventListener("message", socketMessageCallback, false)
-        socket.removeEventListener("close", socketCloseCallback, false)
-          
-      socketMessageCallback = (event) =>
-        buffer = event.data
-        handle = new Float32Array(buffer, 0, 1)[0]
-        if handle == socketHandle
-          detachHandlers()
-          
-          if buffer.byteLength > 4
-            deferred.resolve(new @responseBufferType(buffer, 4))
-          else
-            deferred.reject()
-
-      socketCloseCallback = (event) ->
-        detachHandlers()
-        deferred.reject("socket closed")
+      deferred.handle = socketHandle
       
-      socket.addEventListener("message", socketMessageCallback, false)
-      socket.addEventListener("close", socketCloseCallback, false)
-      socket.send(transmitBuffer.buffer)
+      @pendingRequests.push(deferred)
+      @socket.send(transmitBuffer.buffer)
     
       setTimeout(
-        -> 
-          detachHandlers()
+        => 
+          _.removeElement(@pendingRequests, deferred)
           deferred.reject("timeout")
         @MESSAGE_TIMEOUT
       )
 
       deferred.promise()
+
 
   createPackage : (data) ->
 

  OPEN_TIMEOUT : 5000
  MESSAGE_TIMEOUT : 20000

  constructor : (@url) ->
    _window = window ? self
    @WebSocketImpl = if _window.MozWebSocket then _window.MozWebSocket else _window.WebSocket


  open : ({ @responseBufferType, @requestBufferType }) ->
    @initialize()

  initialize : ->


  fallbackMode : false
  
  constructor : (options) ->

    _.extend(@, options)
    @sender = @defaultSender
    @sender.open(@)
  

  switchToFallback : ->

    unless @fallbackMode
      @sender.close()
      @fallbackMode = true
      @sender = @fallbackSender
      @sender.open(@)


  send : (data) ->

