private void ListenForClients()
        {
            this.tcpListener.Start();

            TcpClient client;

            while (this.keepListening)
            {
                try {
                    client = this.tcpListener.AcceptTcpClient();
                } catch {
                    break;
                }

                iControlClient icClient = new iControlClient(client);
                this.icClients.Add(icClient.IPAddress, icClient);

                Program.Log("[" + icClient.IPAddress + "] Connection accepted. Starting client thread.");

                icClient.Thread = new Thread(new ParameterizedThreadStart(HandleClientCommunication));
                icClient.Thread.Start(icClient);
            }

            Program.Log("Stop listening.");
        }
        private void HandleClientCommunication(object client)
        {
            iControlClient icClient     = (iControlClient)client;
            NetworkStream  clientStream = icClient.TCP.GetStream();

            clientStream.ReadTimeout = 10;

            int bufflen = 4096;

            byte[] message = new byte[bufflen];
            int    bytesRead;

            while (this.keepReceiving && icClient.keepConnected)
            {
                bytesRead = 0;

                try {
                    bytesRead = clientStream.Read(message, 0, bufflen);
                } catch {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }
                ProcessReceivedData(icClient, ParseData(message, bytesRead));
            }

            Program.Log("[" + icClient.IPAddress + "] Connection closed.");

            icClient.TCP.Close();
            this.icClients.Remove(icClient.IPAddress);
        }
        private void ProcessReceivedData(iControlClient icClient, String[] commands)
        {
            Program.Log("[" + icClient.IPAddress + "] >> " + String.Join(" ", commands));

            if (this.CommandReceived != null)
            {
                CommandReceived(this, new CommandReceivedEventArgs(String.Join(" ", commands), icClient));
            }

            NetworkStream clientStream = icClient.TCP.GetStream();
            ASCIIEncoding encoder      = new ASCIIEncoding();

            byte[] buffer = encoder.GetBytes("::ok");
            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();

            icClient.keepConnected = false;
        }
 public CommandReceivedEventArgs(string command, iControlClient client)
 {
     _command          = command;
     _splittedCommands = command.Split(new Char[] { ' ' });
     _client           = client;
 }