private void send(string action, ApplicationIcon toSend)
        {
            toSend.id = AudioSession.getCode(toSend.id);
            string data = action + JSONManager.serialize(toSend);

            send(data);
        }
        public static void sendServerInfo(IPEndPoint clientEP)
        {
            TcpClient server;

            try
            {
                server = new TcpClient();
                server.Connect(clientEP);
            }
            catch (SocketException e)
            {
                Console.WriteLine("Unable to connect to server: " + e.Message);

                return;
            }
            NetworkStream ns = server.GetStream();

            string serverInfo = JSONManager.serialize(Main.info);

            byte[] serverInfoBytes = Encoding.ASCII.GetBytes(serverInfo);

            ns.Write(serverInfoBytes, 0, serverInfoBytes.Length);
            Console.WriteLine("Sent Server Info to: " + clientEP.ToString());
            ns.Close();
            server.Close();
        }
        private void send(string action, AudioSession toSend)
        {
            toSend        = toSend.toCodeId();
            toSend.volume = (float)Math.Round(toSend.volume, 2);
            string data = action + JSONManager.serialize(toSend);

            send(data);
        }
        private void send(string action, AudioSession[] toSend)
        {
            AudioSession[] finalSend = new AudioSession[toSend.Length];
            for (int i = 0; i < toSend.Length; i++)
            {
                toSend[i].volume = (float)Math.Round(toSend[i].volume, 2);
                finalSend[i]     = toSend[i].toCodeId();
            }

            string data = action + JSONManager.serialize(finalSend);

            send(data);
        }
        public static string getEncryptedMessage(string msg, RSAKeyValue key)
        {
            string[] segments          = getSegments(msg);
            string[] encryptedSegments = new string[segments.Length];

            for (int i = 0; i < segments.Length; i++)
            {
                encryptedSegments[i] = Convert.ToBase64String(RSAEncrypt(Encoding.UTF8.GetBytes(segments[i]), key));
            }

            string JSON = JSONManager.serialize(encryptedSegments);

            return(JSON);
        }
        public static string getPublicKey()
        {
            CspParameters param = new CspParameters();

            param.KeyContainerName = "VC_RSA_KEY";
            //Create a new instance of RSACryptoServiceProvider.
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048, param))
            {
                string      xmlKey = RSA.ToXmlString(false);
                RSAKeyValue key    = XMLManager.deserialize <RSAKeyValue>(xmlKey);
                Console.WriteLine("MOD:" + Convert.FromBase64String(key.Modulus).Length *8);
                string jsonKey = JSONManager.serialize(key);

                return(jsonKey);
            }
        }
        private void handleData(string recv)
        {
            Console.WriteLine("\nReceived " + recv);

            if (recv.StartsWith("DEVINFO"))
            {
                recv = recv.Substring("DEVINFO".Length);

                ClientInformation dev = new ClientInformation();
                dev = JSONManager.deserialize <ClientInformation>(recv);
                dev.LastConnected = DateTime.Now;
                dev.IP            = clientEP.Address.ToString();
                dev.Connected     = true;
                device            = dev;
            }
            else
            {
                if (recv.StartsWith("AUTH"))
                {
                    string encryptedPassword     = recv.Remove(0, 4);
                    string decryptedPasswordHash = decryptMessage(encryptedPassword);

                    if (AuthentificationManager.Instance.checkPasswordHash(decryptedPasswordHash))
                    {
                        device.verifiedUntil = DateTime.Now + AuthentificationManager.timeToReAuth;
                        Console.WriteLine("Client authenticated for another period");

                        if (string.IsNullOrEmpty(unprocessedReceiveUntilAuthentication))
                        {
                            return; //No data to handle
                        }
                        else
                        {
                            recv = unprocessedReceiveUntilAuthentication;
                        }

                        unprocessedReceiveUntilAuthentication = "";
                    }
                    else
                    {
                        Console.WriteLine("Wrong password\ngot:" + decryptedPasswordHash + "\nexp:" + AuthentificationManager.Instance.getPasswordHash());
                        send("AUTHWPW");//Code for Wrong Password
                        disconnect();
                    }
                }
                else if (AuthentificationManager.Instance.usesPassword && device.verifiedUntil < DateTime.Now)
                {
                    Console.WriteLine("Client Authentification Expired, requesting new authentication");
                    unprocessedReceiveUntilAuthentication = recv;
                    send("AUTH");
                    return;
                }

                if (device != null && ClientMangager.knownDevices.ContainsKey(device.id))
                {
                    ClientMangager.knownDevices[device.id] = device;
                }
                else
                {
                    if (device != null)
                    {
                        ClientMangager.knownDevices.Add(device.id, device);
                    }
                }
                ClientMangager.saveDevices();
                MainWindow.Instance.NotifyDeviceDatasetChanged();

                if (recv.Equals("GETAUDIOSESSIONS"))
                {
                    int sessionsSent = sendAllAudioSessions();
                    Console.WriteLine("\tSent " + sessionsSent + "Sessions");
                }
                else if (recv.StartsWith("EDIT"))
                {
                    recv = recv.Substring(4);
                    AudioSession session = JSONManager.deserialize <AudioSession>(recv).toSessionId();
                    Main.Instance.audioManager.updateChangedSession(session);
                }
                else if (recv.StartsWith("TRACK"))
                {
                    recv = recv.Substring(5);
                    sendVolumeBlacklist.Add(AudioSession.getSessionId(recv));
                }
                else if (recv.StartsWith("ENDTRACK"))
                {
                    recv = recv.Substring(8);
                    sendVolumeBlacklist.Remove(AudioSession.getSessionId(recv));
                    AudioSession session = Main.Instance.audioManager.getAudioSessionWithID(AudioSession.getSessionId(recv));
                }
                else
                {
                    Console.WriteLine("\nReceived unknown command: " + recv);
                }
            }
        }