private void AcceptConnection() { #region Defining variables TcpClient NewTCPClient; NetworkStream NewTCPStream; string newClientIP; #endregion string guidStr = null; #region Getting connection try { // Here this thread will be waiting till anybody connect to him NewTCPClient = TCPServer.AcceptTcpClient(); newClientIP = ((System.Net.IPEndPoint)(NewTCPClient.Client.RemoteEndPoint)).Address.ToString(); if (!AllowIpList.TryGetValue(newClientIP, out guidStr)) { // Запрет подключения левых IP NewTCPClient.Close(); Console.WriteLine("Refused connection from not allow IP: " + newClientIP); return; } // Getting the network stream NewTCPStream = NewTCPClient.GetStream(); // Отправка уведомления клиенту byte[] HiMessage = new byte[10]; HiMessage = Encoding.GetEncoding(1251).GetBytes("$M$_$ERVER"); NewTCPStream.Write(HiMessage, 0, HiMessage.Length); lock (this) { for (int i = StorekeeperSessions.Count - 1; i >= 0; i--) { DataTerminalSession WorkerSession = StorekeeperSessions[i]; if (WorkerSession.IPAddress == newClientIP) { WorkerSession.CloseChannels(); } } } } catch (Exception exp) { Console.WriteLine("Error during creating TCP client: " + exp.Message); return; } #endregion #region Creating new thread // Here this thread has to create the new thread and give to him new connection DataTerminalSession NewStorekeeperSession = new DataTerminalSession(NewTCPClient, NewTCPStream, DeleteStorekeeperSession, InformAboutUpdateComplete, receiveMessage, new Guid(guidStr)); AddSession(NewStorekeeperSession); //NewStorekeeperSession.Server1CAgent = OneCConnections[newClientIP]; NewStorekeeperSession.IPAddress = newClientIP; NewStorekeeperSession.NeedToUpdate = NeedToUpdateIpList.IndexOf(newClientIP) != -1; NewStorekeeperSession.FullUpdatePath = FullUpdatePath; Thread NewStorekeeperThread = new Thread(NewStorekeeperSession.Start); NewStorekeeperSession.StorekeeperThread = NewStorekeeperThread; NewStorekeeperThread.Name = AllowIpList[newClientIP]; NewStorekeeperThread.IsBackground = true; NewStorekeeperThread.Start(); #endregion }