private void OnClientConnect(ISocket clientSocket)
        {
            if (clientSocket == null)
            {
                return;                                   // socket closed
            }
            FleckLog.Trace($"Client connected from {clientSocket.RemoteIpAddress}:{clientSocket.RemotePort}");
            ListenForClients();

            WebSocketConnection connection = null;

            connection = new WebSocketConnection(
                clientSocket,
                _config,
                bytes => RequestParser.Parse(bytes, _scheme),
                r => HandlerFactory.BuildHandler(r,
                                                 s => connection.OnMessage(s),
                                                 connection.Close,
                                                 b => connection.OnBinary(b),
                                                 b => connection.OnPing(b),
                                                 b => connection.OnPong(b)),
                s => SubProtocolNegotiator.Negotiate(SupportedSubProtocols, s));

            if (IsSecure)
            {
                FleckLog.Debug("Attempting to secure connection...");
                clientSocket.Authenticate(Certificate, EnabledSslProtocols, connection.StartReceiving, e => FleckLog.Error($"Cannot secure the connection: {e.Message}", e));
            }
            else
            {
                connection.StartReceiving();
            }
        }
        private void Read(List <byte> data, byte[] buffer)
        {
            if (!IsAvailable)
            {
                return;
            }

            Socket.Receive(buffer, r =>
            {
                if (r <= 0)
                {
                    FleckLog.Trace("0 bytes read. Closing.");
                    CloseSocket();
                    return;
                }
                FleckLog.Trace(r + " bytes read");
                var readBytes = buffer.Take(r);
                if (Handler != null)
                {
                    Handler.Receive(readBytes);
                }
                else
                {
                    data.AddRange(readBytes);
                    CreateHandler(data);
                }

                Read(data, buffer);
            },
                           HandleReadError);
        }
 private Task SendBytes(byte[] bytes, Action callback = null)
 {
     return(Socket.Send(bytes, () =>
     {
         FleckLog.Trace("Sent " + bytes.Length + " bytes");
         callback?.Invoke();
     },
                        e =>
     {
         if (e is IOException)
         {
             FleckLog.Error("Failed to send. Disconnecting.", e);
         }
         else
         {
             FleckLog.Error("Failed to send. Disconnecting.", e);
         }
         CloseSocket();
     }));
 }
        public void Start(Action <IWebSocketConnection> config)
        {
            var ipLocal = new IPEndPoint(_locationIP, Port);

            ListenerSocket.Bind(ipLocal);
            ListenerSocket.Listen(1000);
            Port = ((IPEndPoint)ListenerSocket.LocalEndPoint).Port;
            if (_scheme == "wss")
            {
                if (Certificate == null)
                {
                    FleckLog.Error("Scheme cannot be 'wss' without a Certificate");
                    return;
                }

                if (EnabledSslProtocols == SslProtocols.None)
                {
                    EnabledSslProtocols = SslProtocols.Tls;
                    FleckLog.Trace("Using default TLS 1.0 security protocol.");
                }
            }
            ListenForClients();
            _config = config;
        }