/// <summary> /// Establishes the connection to the server (if client) or lisens to new incomming ones (if server). /// </summary> /// <param name="server">If the instance shall act as server or not.</param> /// <returns></returns> public static bool StartListening(bool server, string password) { // Create a TCP/IP socket. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Thread pongConnectionMain; if (server) { try { socket.Bind(ipEndPoint); socket.Listen(4); PlayerID = -1; pongConnectionMain = new Thread(ListenToClients); pongConnectionMain.IsBackground = false; pongConnectionMain.Start(); Pong.InitializeGame(); PongConnection.password = password; } catch (Exception e) { Console.WriteLine(e.Message); return(false); } } else { try { socket.Connect(ipEndPoint); PlayerID = 0; pongConnectionMain = new Thread(ListenToServer); pongConnectionMain.IsBackground = false; pongConnectionMain.Start(); Thread.Sleep(10); Console.WriteLine("Sending first message"); socket.Send(Encoding.UTF8.GetBytes("?" + State_Menu.Singleton.input)); Pong.InitializeGame(); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } } return(true); }