public string addSingleNode(string name, int port_)
        {
            try {

                Router r = new Router();
                r.identifier = name;
                r.port = port_;
                r.socket = new RouterSocket(r.port, r.identifier);
                r.socket.TurnOn();
                r.connected = true;
                nodelist.Add(r.identifier);

                rList.Add(r);
                mainWindow.nodeBox.ItemsSource = nodelist;

                mainWindow.appendConsole(string.Format("Connection to {0} succeeded", r.identifier), null, null);

            }
            catch (Exception e)
            {
                return "Error while trying to connect to "+name;
            }
            try
            {
                // TODO
               // ConfigReader.addNewElement(name, port_);
            }
            catch(Exception e)
            {
                return "Error while trying to add to config file";
            }
            return name + " added";
        }
        public SocketHandler(List<int> portList, MainWindow main)
        {
            rList = new List<Router>();
            cList = new List<Router>();
            mainWindow = main;
            nodelist = new List<string>();
            clientNameList = new List<string>();
            endPointsList = new List<Router>();

            foreach(int port_ in portList)
            {
                Router temp = new Router { port = port_, connected = false };
                endPointsList.Add(temp);
            }

            discoverPorts(endPointsList);
        }
        public SocketHandler(List<int> portList, MainWindow main, List<string> modules, List<string> conteners)
        {
            rList = new List<Router>();
            cList = new List<Router>();
            availableConteners = conteners;
            availableModules = modules;
            mainWindow = main;
            nodelist = new List<string>();
            clientNameList = new List<string>();
            endPointsList = new List<Router>();

            foreach(int port_ in portList)
            {
                Router temp = new Router { port = port_, connected = false };
                endPointsList.Add(temp);
            }

            discoverPorts(endPointsList);
        }
        private string identify(Router r)
        {
            RouterSocket targetSocket = r.socket;
            byte[] bytes = new byte[100000];
            string response = null;
            Socket conversationSocket = null;
            try {

                targetSocket.InitConversation();
                conversationSocket = targetSocket.GetSocket();
            }
            catch (Exception e)
            {
                mainWindow.appendConsole("Nie można było nawiązać połączania na porcie "+r.port+" ,błąd: "+e.Message, null, null);
            }
            try
            {
                byte[] msg = Encoding.ASCII.GetBytes("identify|");

                conversationSocket.Send(msg);

                bytes = new byte[1024];
                int bytesrec = 0;

                if (conversationSocket.Poll(1000000, SelectMode.SelectRead))
                {
                    bytesrec = conversationSocket.Receive(bytes); // This call will not block
                }
                else
                {
                    mainWindow.appendConsole("Nie można było nawiązać połączenia na porcie " + r.socket.port + ". Spróbuj ponownie", null, null);

                }
                response += Encoding.ASCII.GetString(bytes, 0, bytesrec);

                r.connected = true;

                string[] responseArray = response.Split('|');

                if (responseArray[0].Equals("router"))
                {
                    string routerName = responseArray[1];
                    r.nodetype = "router";
                    return routerName;
                }
                else if (responseArray[0].Equals("client"))
                {
                    string clientName = responseArray[1];
                    r.nodetype = "client";
                    return clientName;
                }
                else
                {
                    mainWindow.appendConsole("Endpoint " + r.port + " podał nieprawidłową odpowiedź. Spróbuj ponownie", null, null);
                    return null;
                }
            }
            catch (Exception e)
            {
                mainWindow.appendConsole("Błąd podczas identyfikacji: " + e.Message,null,null);
                return null;
            }
        }
        private string identify(Router r)
        {
            RouterSocket targetSocket = r.socket;
            byte[] bytes = new byte[100000];
            string response = null;
            Socket conversationSocket = null;
            try {

                targetSocket.InitConversation();
                conversationSocket = targetSocket.GetSocket();
            }
            catch (Exception e)
            {
                mainWindow.appendConsole("Could not connect to specified endpoint", null, null);
            }
            try
            {
                byte[] msg = Encoding.ASCII.GetBytes("identify|");

                conversationSocket.Send(msg);

                bytes = new byte[1024];
                int bytesrec = 0;

                if (conversationSocket.Poll(1000000, SelectMode.SelectRead))
                {
                    bytesrec = conversationSocket.Receive(bytes); // This call will not block
                }
                else
                {
                    mainWindow.appendConsole("Could not reach endpoint on port " + r.socket.port + ". Try again", null, null);

                }
                response += Encoding.ASCII.GetString(bytes, 0, bytesrec);

                r.connected = true;

                string[] responseArray = response.Split('|');

                if (responseArray[0].Equals("router"))
                {
                    string routerName = responseArray[1];
                    r.nodetype = "router";
                    return routerName;
                }
                else if (responseArray[0].Equals("client"))
                {
                    string clientName = responseArray[1];
                    r.nodetype = "client";
                    return clientName;
                }
                else
                {
                    mainWindow.appendConsole("Endpoint " + r.port + " provided corrupted response. Try again", null, null);
                    return null;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }