public async Task<bool> Connect(string hostname, int port)
{
if(ServerConnectionStarting != null)
ServerConnectionStarting(this, new ServerConnectionEventArgs{Message = "Connecting to " + hostname + ":" + port.ToString() + "...", Status = ServerConnectionStatus.Connecting, Timestamp = DateTime.Now});
try
{
TcpClient = new TcpClient();
TcpClient.NoDelay = true;
await TcpClient.ConnectAsync(hostname, port);
}
catch
{
if(ServerConnectionFailed != null)
ServerConnectionFailed(this, new ServerConnectionEventArgs{Message = "Failed to connect! Make sure the server is running and that your hostname and port are correct.", Status = ServerConnectionStatus.Disconnected, Timestamp = DateTime.Now});
}
if(IsConnected)
{
TcpClientStream = TcpClient.GetStream();
if(ServerConnectionSucceeded != null)
ServerConnectionSucceeded(this, new ServerConnectionEventArgs{Message = "Successfully connected.", Status = ServerConnectionStatus.Connected, Timestamp = DateTime.Now});
//Keepalive.Reset();
return true;
}
else return false;
}