private void InitializeConnection()
{
SocketPermission Perm = new SocketPermission(System.Security.Permissions.PermissionState.None);
ipAddr = IPAddress.Parse(txtIp.Text);
Perm.AddPermission(NetworkAccess.Accept, TransportType.Tcp, txtIp.Text,9050) ; //1986)
Perm.AddPermission(NetworkAccess.Connect, TransportType.Tcp, txtIp.Text, 9050);
// Parse the IP address from the TextBox into an IPAddress object
// Start a new TCP connections to the chat server
tcpServer = new TcpClient();
tcpServer.Connect(ipAddr,9050);
// Helps us track whether we're connected or not
Connected = true;
// Prepare the form
UserName = txtUser.Text;
// Disable and enable the appropriate fields
txtIp.Enabled = false;
txtUser.Enabled = false;
txtMessage.Enabled = true;
btnSend.Enabled = true;
btnConnect.Text = "Disconnect";
// Send the desired username to the server
swSender = new StreamWriter(tcpServer.GetStream());
swSender.WriteLine(txtUser.Text);
swSender.Flush();
// Start the thread for receiving messages and further communication
thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
thrMessaging.Start();
}