             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
             }

                    }
                }
                let error = self.errorWithDetail("connection closed by server", code: code)
                if let disconnect = self.disconnectedBlock {
                    disconnect(error)
                }
                self.delegate?.websocketDidDisconnect(self, error: error)
                writeError(code)
                return
            }

                        let errCode = InternalErrorCode.OutputStreamWriteError.rawValue
                        error = self.errorWithDetail("output stream error during write", code: errCode)
                    }
                    self.doDisconnect(error)
                    break
                } else {
                    total += len!
