     @_connectionPromise
 
   _leaveRoom: () ->
-    @ws.send JSON.stringify(type: 'leave')
-    @ws.close()
-    @ws = null
-    @_connectionPromise = null
+    [ws, @ws, @_connectionPromise] = [@ws, undefined, undefined]
+    ws.onclose = undefined
+    ws.send JSON.stringify(type: 'leave')
+    ws.close()
     @emit 'm2fa.room.left'
 
   _onOpen: (e) ->

    @_leaveRoom()

  _joinRoom: (pairingId) ->
    return @_connectionPromise if @ws?
    d = Q.defer()
    @_connectionPromise = d.promise
    @ws = new WebSocket(@constructor.BASE_URL)

      when "challenge" then @_onChallenge(data)

  _onClose: (e) ->
    [@ws.onclose, @ws.onmessage] = [undefined, undefined]
    [@ws, @_connectionPromise] = [undefined, undefined]
    @_joinRoom()

  _send: (data) ->
    @_joinRoom().then(=> @_send(data)) if ! @_connectionPromise?
    @_connectionPromise.then =>
      @ws.send(data)

