public SSPClient[] GetClients()
 {
     lock (Clients)
     {
         SSPClient[] c = new SSPClient[Clients.Count];
         Clients.Values.CopyTo(c, 0);
         return(c);
     }
 }
        private void AsyncAction(IAsyncResult result)
        {
            Socket AcceptedSocket = null;

            try
            {
                AcceptedSocket = TcpServer.EndAccept(result);
            }
            catch { TcpServer.BeginAccept(AsyncAction, null); }

            //keep receiving connections
            SSPClient client = serverProperties.GetNewClient();

            client.Handle                   = AcceptedSocket;
            client.Connection               = new Connection(client);
            client.RemoteIp                 = ((IPEndPoint)AcceptedSocket.RemoteEndPoint).Address.ToString();
            client.Connection.ClientId      = Random.NextDecimal();//new Random(DateTime.Now.Millisecond).Next(0, 1000); //tempId++;//
            client.ServerAllowsReconnecting = client.ReconnectAtDisconnect;
            client.Server                   = this;
            client.Connection.State         = ConnectionState.Open;

            lock (Clients)
            {
                while (Clients.ContainsKey(client.ClientId))
                {
                    client.Connection.ClientId = Random.NextDecimal();
                }
                Clients.Add(client.ClientId, client);
            }

            //accept new client
            TcpServer.BeginAccept(AsyncAction, null);

            try
            {
                if (!client.ServerHandshake(serverProperties, this.UdpServer, GetClients, KeyHandler))
                {
                    lock (Clients)
                    {
                        if (Clients.ContainsKey(client.ClientId))
                        {
                            Clients.Remove(client.ClientId);
                        }
                    }
                    client.Disconnect(DisconnectReason.HandShakeFailed);
                    return;
                }
            }
            catch (Exception ex)
            {
                onException(ex);
                client.Disconnect(DisconnectReason.HandShakeFailed);
                return;
            }

            try
            {
                client.onShareClasses();
            }
            catch (Exception ex)
            {
                client.onException(ex, ErrorType.UserLand);
            }

            client.StartReceiver();

            try
            {
                client.SharedClientRoot = client.GetSharedClass <ISharedClientRoot>("ROOTSOCKET_CLIENT");
            }
            catch (Exception ex)
            {
                //the shared class must exist
                client.Disconnect();
                return;
            }

            if (client.State != ConnectionState.Reconnecting)
            {
                client.onClientConnect();
            }
            onConnectionAccept(client);
        }
 public abstract bool onPeerCreateDnsRequest(string DnsName, SSPClient Requestor);
 public abstract bool onPeerConnectionRequest(SSPClient FromClient, SSPClient ToClient);
 public abstract bool onAuthentication(SSPClient client, string Username, string Password);
 public abstract void onConnectionClosed(SSPClient client);
 public abstract void onConnectionAccept(SSPClient client);