public void TryConnect(IPAddress iPAddress, int port, string login, string password, MainWindow main) { Task.Run(() => { this.main = main; try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; IPEndPoint ipPoint = new IPEndPoint(iPAddress, port); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(ipPoint); using (var networkStream = new NetworkStream(socket, true)) using (var sslStream = new SslStream(networkStream, false, ValidateServerCertificate)) { this.SslStream = sslStream; sslStream.AuthenticateAsClient("SecureServer"); AutentificationData data; data.login = login; data.password = password; byte[] SendData = Encoding.UTF8.GetBytes("auth:" + JsonConvert.SerializeObject(data) + "<EOF>"); sslStream.Write(SendData); sslStream.Flush(); string serverMessage = ReadMessage(); if (main.DisplayRoleInterface(serverMessage)) { StartHandlingAnswer(); } main.Dispatcher.Invoke(() => main.TryEnter.IsEnabled = true); }; socket.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } }); }