             if((isMasked > 0 || (RSVMask & buffer[0]) > 0) && receivedOpcode != OpCode.Pong.rawValue) {
                 let errCode = CloseCode.ProtocolError.rawValue
                 let error = self.errorWithDetail("masked and rsv data is not currently supported", code: errCode)
-                if let disconnect = self.disconnectedBlock {
-                    disconnect(error)
-                }
-                self.delegate?.websocketDidDisconnect(self, error: error)
+                self.doDisconnect(error)
                 writeError(errCode)
                 return
             }

        outputStream = nil
        isRunLoop = false
        connected = false
        dispatch_async(queue,{
            if let disconnectBlock = self.disconnectedBlock {
                disconnectBlock(error)
            }
            self.delegate?.websocketDidDisconnect(self, error: error)
        })
    }
    
    ///handles the incoming bytes and sending them to the proper processing method

                    }
                }
                let error = self.errorWithDetail("connection closed by server", code: code)
                self.doDisconnect(error)
                writeError(code)
                return
            }
