private void OnAccept(Socket client)
        {
            IPEndPoint        ep1   = client.RemoteEndPoint as IPEndPoint;
            SmartSocketClient proxy = new SmartSocketClient(this, client, this.resolver)
            {
                Name       = ep1.ToString(),
                ServerName = SmartSocketClient.FindLocalHostName()
            };

            proxy.Disconnected += this.OnClientDisconnected;

            SmartSocketClient[] snapshot = null;

            lock (this.clients)
            {
                snapshot = this.clients.ToArray();
            }

            foreach (SmartSocketClient s in snapshot)
            {
                IPEndPoint ep2 = s.Socket.RemoteEndPoint as IPEndPoint;
                if (ep1 == ep2)
                {
                    // can only have one client using this end point.
                    this.RemoveClient(s);
                }
            }

            lock (this.clients)
            {
                this.clients.Add(proxy);
            }

            if (this.ClientConnected != null)
            {
                this.ClientConnected(this, proxy);
            }
        }
        private void UdpListenerThread()
        {
            var           localHost = SmartSocketClient.FindLocalHostName();
            List <string> addresses = SmartSocketClient.FindLocalIpAddresses();

            if (localHost == null || addresses.Count == 0)
            {
                return; // no network.
            }

            IPEndPoint remoteEP = new IPEndPoint(GroupAddress, GroupPort);

            this.udpListener = new UdpClient(GroupPort);
            this.udpListener.JoinMulticastGroup(GroupAddress);
            while (true)
            {
                byte[] data = this.udpListener.Receive(ref remoteEP);
                if (data != null)
                {
                    BinaryReader reader = new BinaryReader(new MemoryStream(data));
                    int          len    = reader.ReadInt32();
                    string       msg    = reader.ReadString();
                    if (msg == this.serviceName)
                    {
                        // send response back with info on how to connect to this server.
                        IPEndPoint   localEp = (IPEndPoint)this.listener.LocalEndPoint;
                        string       addr    = localEp.ToString();
                        MemoryStream ms      = new MemoryStream();
                        BinaryWriter writer  = new BinaryWriter(ms);
                        writer.Write(addr.Length);
                        writer.Write(addr);
                        writer.Flush();
                        byte[] buffer = ms.ToArray();
                        this.udpListener.Send(buffer, buffer.Length, remoteEP);
                    }
                }
            }
        }