internal void OnReceived(BaseSocketConnection connection, byte[] buffer)
        {
            if (Disposed || !connection.Active)
                return;

            try
            {
                ISocketSecurityProvider socketSecurityProvider = new SocketRSACryptoProvider(connection, buffer);

                switch (connection.Context.EventProcessing)
                {
                    case EventProcessing.epEncrypt:

                        if (connection.Context.Host.Context.HostType == HostType.htServer)
                        {
                            //----- Deserialize authentication message

                            try
                            {
                                MemoryStream m = socketSecurityProvider.DecryptForServer();

                                BeginSend(connection, m.ToArray(), false);
                            }
                            catch (SymmetricAuthenticationException ex)
                            {
                                FireOnException(connection, ex);
                            }
                        }
                        else
                        {
                            //----- Deserialize authentication message
                            try
                            {
                                AuthMessage am = socketSecurityProvider.DecryptForClient();
                            }
                            catch (SymmetricAuthenticationException ex)
                            {
                                FireOnException(connection, ex);
                            }
                        }

                        break;

                    case EventProcessing.epProxy:

                        ProxyInfo proxyInfo = ((SocketConnector)connection.Context.Creator).ProxyInfo;
                        ProxyUtils.GetProxyResponseStatus(proxyInfo, buffer);

                        if (proxyInfo.Completed)
                        {
                            InitializeConnection(connection);
                        }
                        else
                        {
                            IPEndPoint endPoint = ((SocketConnector)connection.Context.Creator).Context.RemotEndPoint;
                            byte[] proxyBuffer = ProxyUtils.GetProxyRequestData(proxyInfo, endPoint);

                            connection.BeginSend(proxyBuffer);
                        }

                        break;
                }
            }
            catch (Exception ex)
            {
                FireOnException(connection, ex);
            }
        }
        /// <summary>
        /// Initializes the connection
        /// </summary>
        /// <param name="connection"></param>
        internal void OnConnected(BaseSocketConnection connection)
        {
            if (Disposed || !connection.Active)
                return;

            try
            {
                switch (connection.Context.EventProcessing)
                {
                    case EventProcessing.epEncrypt:

                        switch (connection.Context.Creator.Context.EncryptType)
                        {
                            case EncryptType.etRijndael:

                                if (connection.Context.Host.Context.HostType == HostType.htClient)
                                {

                                    ISocketSecurityProvider socketSecurityProvider = new SocketRSACryptoProvider(connection, null);
                                    MemoryStream m = socketSecurityProvider.EcryptForClient();
                                    connection.BeginSend(m.ToArray());

                                }
                                else
                                {

                                    connection.BeginReceive();

                                }

                                break;

                            case EncryptType.etSSL:

                                if (connection.Context.Host.Context.HostType == HostType.htClient)
                                {

                                    //----- Get SSL items
                                    X509Certificate2Collection certs = null;
                                    string serverName = null;
                                    bool checkRevocation = true;

                                    connection.Context.Creator.Context.CryptoService.OnSSLClientAuthenticate(connection, out serverName, ref certs, ref checkRevocation);

                                    //----- Authenticate SSL!
                                    SslStream ssl = new SslStream(new NetworkStream(connection.Context.SocketHandle), true, new RemoteCertificateValidationCallback(connection.Context.Creator.ValidateServerCertificateCallback));

                                    if (certs == null)
                                    {
                                        ssl.BeginAuthenticateAsClient(serverName, new AsyncCallback(SslAuthenticateCallback), new AuthenticateCallbackData(connection, ssl, HostType.htClient));
                                    }
                                    else
                                    {
                                        ssl.BeginAuthenticateAsClient(serverName, certs, System.Security.Authentication.SslProtocols.Tls, checkRevocation, new AsyncCallback(SslAuthenticateCallback), new AuthenticateCallbackData(connection, ssl, HostType.htClient));
                                    }

                                }
                                else
                                {

                                    //----- Get SSL items!
                                    X509Certificate2 cert = null;
                                    bool clientAuthenticate = false;
                                    bool checkRevocation = true;

                                    connection.Context.Creator.Context.CryptoService.OnSSLServerAuthenticate(connection, out cert, out clientAuthenticate, ref checkRevocation);

                                    //----- Authneticate SSL!
                                    SslStream ssl = new SslStream(new NetworkStream(connection.Context.SocketHandle));
                                    ssl.BeginAuthenticateAsServer(cert, clientAuthenticate, System.Security.Authentication.SslProtocols.Default, checkRevocation, new AsyncCallback(SslAuthenticateCallback), new AuthenticateCallbackData(connection, ssl, HostType.htServer));

                                }

                                break;
                        }

                        break;

                    case EventProcessing.epProxy:

                        ProxyInfo proxyInfo = ((SocketConnector)connection.Context.Creator).ProxyInfo;
                        IPEndPoint endPoint = ((SocketConnector)connection.Context.Creator).Context.RemotEndPoint;
                        byte[] proxyBuffer = ProxyUtils.GetProxyRequestData(proxyInfo, endPoint);

                        connection.BeginSend(proxyBuffer);

                        break;
                }
            }
            catch (Exception ex)
            {
                FireOnException(connection, ex);
            }
        }