private void ServerThreadRoutine(IPAddress serverIpAddress)
        {
            // Create the server model.
            ServerModel newServerConnection = new ServerModel(serverIpAddress, this);

            // Actually starts the server routine, connecting the Server and receiving data.
            newServerConnection.StartClient();
        }
        public void AddServer(ServerModel ServerToAdd, IPAddress IpAddressToAdd)
        {
            // Add the server to the servers list.
            // IpConnections is also bound to the ServersComboBox
            IpConnections.Add(IpAddressToAdd);

            // Add the server Ip Address to the IpConnection list.
            ServerConnections.Add(ServerToAdd);
        }
        // Disconnect the client from the currently selected Server
        public void DisconnectFromServer(IPAddress ipServerToDisconnect)
        {
            if (ipServerToDisconnect != null)
            {
                ServerModel serverToDisconnect = null;

                // From the Ip look-up the server.
                foreach (ServerModel server in ServerConnections)
                {
                    if (server.ServerIpAddress.Equals(ipServerToDisconnect))
                    {
                        serverToDisconnect = server;
                        // Stop the serverTimer if the disconnect is called from the GUI
                        if (serverToDisconnect.HasToStopTimer)
                        {
                            serverToDisconnect.ServerTimer.Stop();
                        }
                        break;
                    }
                }

                if (serverToDisconnect != null)
                {
                    // I found the server I want to disconnect

                    if (serverToDisconnect.Equals(CurrentSelectedServer))
                    {
                        // I want to disconnect the currentSelectedServer
                        serverToDisconnect.CloseClient();
                        // The function calls already the RemoveServer function

                        // Remove the server information form the GUI
                        NotificationMessage = "Server " + ipServerToDisconnect.ToString() + " disconnesso.";

                        // If there are other servers, select the first of them
                        if (ServerConnections.Count() > 0)
                        {
                            // Select the first server.
                            CurrentSelectedServer    = ServerConnections.First();
                            CurrentSelectedIpAddress = CurrentSelectedServer.ServerIpAddress;
                            CurrentSelectedProcess   = null;
                            TheProcessesList         = CurrentSelectedServer.TheProcesses;
                            // Update the GUI
                            RaisePropertyChanged("TheProcessesList");
                            RaisePropertyChanged("CurrentSelectedIpAddress");
                            RaisePropertyChanged("CurrentSelectedServer");
                            RaisePropertyChanged("CurrentSelectedProcess");
                            RaisePropertyChanged("IpConnections");
                        }
                        else // There are no more connected servers.
                        {
                            CurrentSelectedServer    = null;
                            CurrentSelectedProcess   = null;
                            CurrentSelectedIpAddress = null;
                            TheProcessesList         = null;
                            // Update the GUI
                            RaisePropertyChanged("TheProcessesList");
                            RaisePropertyChanged("CurrentSelectedIpAddress");
                            RaisePropertyChanged("CurrentSelectedServer");
                            RaisePropertyChanged("CurrentSelectedProcess");
                            RaisePropertyChanged("IpConnections");
                        }
                    }
                    else
                    {
                        // The server I want to disconnect is just one among the others.
                        // Close takes care of calling the removeProcessfromApplications and the remove server.
                        serverToDisconnect.CloseClient();
                        // Update the GUI
                        RaisePropertyChanged("TheProcessesList");
                        RaisePropertyChanged("CurrentSelectedIpAddress");
                        RaisePropertyChanged("CurrentSelectedServer");
                        RaisePropertyChanged("CurrentSelectedProcess");
                        RaisePropertyChanged("IpConnections");
                    }
                }
                else // I haven't found the server I am looking for
                {
                    System.Windows.Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        // Open the Error Window
                        Views.ErrorWindowView errView         = new Views.ErrorWindowView();
                        ErrorWindowViewModel errWindViewModel = new ErrorWindowViewModel("Non è stato trovato il server da disconnettere.");
                        errWindViewModel.ClosingRequest      += errView.Close;
                        errView.DataContext = errWindViewModel;
                        errView.Show();
                    });
                }
            }
            else // No server is selected
            {
                // Open the Error Window
                Views.ErrorWindowView errView          = new Views.ErrorWindowView();
                ErrorWindowViewModel  errWindViewModel = new ErrorWindowViewModel("Non è selezionato nessun server.");
                errWindViewModel.ClosingRequest += errView.Close;
                errView.DataContext              = errWindViewModel;
                errView.Show();
            }
        }
        // Remove a Server from the data structures.
        public void RemoveServer(ServerModel ServerToRemove, IPAddress IpAddressToRemove)
        {
            // Iterate for each process in the server and remove it from the applications listed in the applicationList of the viewModel.
            foreach (ProcessModel proc in ServerToRemove.TheProcesses)
            {
                ServerToRemove.RemoveProcessFromApplications(proc);
                // The function already iterates for all the application in the list and checks if the application has still at least a server running it.
                // It also takes care of raising the propertyChanged.
            }

            // Remove it from the IpConnections.
            if (IpConnections.Contains(IpAddressToRemove))
            {
                System.Windows.Application.Current.Dispatcher.Invoke((Action) delegate
                {
                    IpConnections.Remove(IpAddressToRemove);
                    RaisePropertyChanged("IpConnections");
                });
            }
            // Remove it from the ServerConnectons.
            if (ServerConnections.Contains(ServerToRemove))
            {
                ServerConnections.Remove(ServerToRemove);
            }

            // Check that there is the entry correspondent witht the serverIpAddress.
            lock (syncListOfConnectionThreads)
            {
                if (_listOfConnectionThreads != null && _currentSelectedServer != null)
                {
                    if (_listOfConnectionThreads.ContainsKey(_currentSelectedServer.ServerIpAddress))
                    {
                        // Abort the thread running the connection.
                        _listOfConnectionThreads[_currentSelectedServer.ServerIpAddress].Abort();
                        // Remove the thread from the dictionary.
                        _listOfConnectionThreads.Remove(_currentSelectedServer.ServerIpAddress);
                    }
                }
            }

            // If there are other servers, select the first of them
            if (ServerConnections.Count() > 0)
            {
                // Select the first server.
                CurrentSelectedServer    = ServerConnections.First();
                CurrentSelectedIpAddress = CurrentSelectedServer.ServerIpAddress;
                CurrentSelectedProcess   = null;
                TheProcessesList         = CurrentSelectedServer.TheProcesses;
                // Update the GUI
                RaisePropertyChanged("TheProcessesList");
                RaisePropertyChanged("CurrentSelectedIpAddress");
                RaisePropertyChanged("CurrentSelectedServer");
                RaisePropertyChanged("CurrentSelectedProcess");
            }
            else
            {
                // There are no more connected servers.
                CurrentSelectedServer    = null;
                CurrentSelectedProcess   = null;
                CurrentSelectedIpAddress = null;
                TheProcessesList         = null;
                RaisePropertyChanged("TheProcessesList");
            }
        }