public void Connect(IPAddress ipAddress, int port) { this.textPlainTCPChannel.Connect(ipAddress, port); //Read a persistent private key or create a new //TODO: See for domain name server rather than ip. var ppk = this.IPersistentPrivateKeyProvider.Read(ipAddress.ToString(), port); PublicKey publicKeyToSend = null; if (ppk == null)//New key to generate in server. { this.asymetricForDecrypt.GenerateKeys(); this.thisPrivateKey = this.asymetricForDecrypt.GetPrivateKey();//To Decrypt Server Messages publicKeyToSend = asymetricForDecrypt.GetPublicKey(); } else //Using existing key. { publicKeyToSend = new PublicKey { yExponent = ppk.PrivateKey.yExponent, yModulus = ppk.PrivateKey.yModulus }; this.thisPrivateKey = ppk.PrivateKey; } bool IsKeyFound = ppk != null; SendPublicKey(publicKeyToSend, IsKeyFound, false); //Should receive the public key this.Receive(littleBuffer, 0, littleBuffer.Length); //if (ppk == null)//No save yet { PersistentPrivateKey ppkNew = new PersistentPrivateKey { Port = port, Server = ipAddress.ToString(), PrivateKey = this.thisPrivateKey, PublicKeyRemote = this.remotePublicKey, FromServer = false }; IPersistentPrivateKeyProvider.Save(ppkNew); } }