public cTcpSocketServer(cTcpSocket tcpSocket, int iListenPort)
        {
            TcpSocket  = tcpSocket;
            ListenPort = iListenPort;


            if (iListenPort < 1 || iListenPort > 65535)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Portnumber is invalid");
            }
            else
            {
                try
                {
                    listener = new TcpListener(IPAddress.Any, ListenPort);
                    listener.Start();
                    lock (this)
                    {
                        cTcpSocketServerThread ServerThread = new cTcpSocketServerThread(this);
                        new Thread(new ThreadStart(ServerThread.RunThread)).Start();
                        ServerSockets.Add(ServerThread);
                    }
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Waiting for connection at port " + ListenPort.ToString());
                }
                catch (SocketException se)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Could not create socket server thread: " + se.Message);
                }
            }
        }
        public void RunThread()
        {
            try
            {
                tcpClient = SocketServer.listener.AcceptTcpClient();

                lock (SocketServer)
                {
                    KeepAliveTimer = 0;

                    socketStream = new nsRSMPGS.cSocketStream(cHelper.IsSettingChecked("UseEncryption"));

                    cTcpSocketServerThread ServerThread = new cTcpSocketServerThread(SocketServer);
                    new Thread(new ThreadStart(ServerThread.RunThread)).Start();
                    SocketServer.ServerSockets.Add(ServerThread);
                }

                if (socketStream.InitializeAsServerAndGetStream(tcpClient, RSMPGS.EncryptionSettings))
                {
                    encoding = Encoding.GetEncoding("Windows-1252");

                    sClientIP = ((IPEndPoint)(tcpClient.Client.RemoteEndPoint)).Address.ToString() + ":" + ((IPEndPoint)(tcpClient.Client.RemoteEndPoint)).Port.ToString();

                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Client connection was accepted " + sClientIP);

                    tcpClient.NoDelay = RSMPGS.MainForm.ToolStripMenuItem_DisableNagleAlgorithm.Checked;

                    RSMPGS.MainForm.BeginInvoke(RSMPGS.MainForm.DelegateSocketWasConnected);

                    while (SocketServer.TcpSocket.ThreadShouldRun)
                    {
                        if (RSMPGS.RSMPConnection.ReadBytesAndParsePacket(ref socketStream, ref tcpClient, ref inBuffer, ref inBufferLength) == false)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (socketStream != null)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Some problem occured (" + sClientIP + "), closed socket: " + e.Message);
                }
            }

            SocketServer.Disconnect();

            if (sClientIP.Length > 0)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Warning, "Client connection " + sClientIP + " was closed");
            }

            lock (SocketServer)
            {
                SocketServer.ServerSockets.Remove(this);
            }
        }
        public string RemoteClient()
        {
            string sRemoteClient = "";

            if (ServerSockets.Count > 0)
            {
                cTcpSocketServerThread SocketServerThread = (cTcpSocketServerThread)ServerSockets[0];
                if (ConnectionStatus() == cTcpSocket.ConnectionStatus_Connected || ConnectionStatus() == cTcpSocket.ConnectionStatus_Connecting)
                {
                    sRemoteClient = SocketServerThread.sClientIP;
                }
            }
            return(sRemoteClient);
        }
        public int ConnectionStatus()
        {
            int iConnectionStatus = cTcpSocket.ConnectionStatus_Unknown;

            lock (this)
            {
                if (ServerSockets.Count > 0)
                {
                    cTcpSocketServerThread SocketServerThread = (cTcpSocketServerThread)ServerSockets[0];
                    if (SocketServerThread.tcpClient == null)
                    {
                        iConnectionStatus = cTcpSocket.ConnectionStatus_Disconnected;
                    }
                    else
                    {
                        if (SocketServerThread.tcpClient.Connected)
                        {
                            if (SocketServerThread.socketStream != null)
                            {
                                if (SocketServerThread.socketStream.IsConnected())
                                {
                                    iConnectionStatus = cTcpSocket.ConnectionStatus_Connected;
                                }
                                else
                                {
                                    iConnectionStatus = cTcpSocket.ConnectionStatus_Connecting;
                                }
                            }
                            else
                            {
                                iConnectionStatus = cTcpSocket.ConnectionStatus_Connecting;
                            }
                        }
                        else
                        {
                            iConnectionStatus = cTcpSocket.ConnectionStatus_Connecting;
                        }
                    }
                }
            }
            return(iConnectionStatus);
        }