using System.Threading.Tasks;
namespace System.Net.Sockets;
internal static class TcpClientExtensions
{
///
/// Connects the client to a remote TCP host using the specified IP endpoint as an asynchronous operation with a
/// timeout.
///
/// Endpoint where the client needs to connect to.
///
/// Timeout within the client needs to connect to the remote TCP host.
///
/// Returns if the connection was established before the timeout exceeded.
public static async Task ConnectAsync(this TcpClient client, IPEndPoint endpoint, int connectionTimeoutMilliseconds)
{
var connectTask = client.ConnectAsync(endpoint.Address, endpoint.Port);
return await Task.WhenAny(connectTask, Task.Delay(connectionTimeoutMilliseconds)) == connectTask;
}
}