Socket.BeginConnect Method (IPAddress, Int32, AsyncCallback, Object)

Note: This method is new in the .NET Framework version 2.0.

Begins an asynchronous request for a remote host connection. The host is specified by an IPAddress and a port number.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

public IAsyncResult BeginConnect (
	IPAddress address,
	int port,
	AsyncCallback requestCallback,
	Object state
public IAsyncResult BeginConnect (
	IPAddress address, 
	int port, 
	AsyncCallback requestCallback, 
	Object state
public function BeginConnect (
	address : IPAddress, 
	port : int, 
	requestCallback : AsyncCallback, 
	state : Object
) : IAsyncResult



The IPAddress of the remote host.


The port number of the remote host.


An AsyncCallback delegate that references the method to invoke when the connect operation is complete.


A user-defined object that contains information about the connect operation. This object is passed to the requestCallback delegate when the operation is complete.

Return Value

An IAsyncResult that references the asynchronous connection.

Exception typeCondition


address is a null reference (Nothing in Visual Basic).


An error occurred when attempting to access the socket. See the Remarks section for more information.


The Socket has been closed.


The Socket is not in the socket family.


The port number is not valid.


The length of address is zero.


The Socket is Listening.

The asynchronous BeginConnect operation must be completed by calling the EndConnect method. Typically, the method is invoked by the requestCallback delegate.

This method does not block until the operation is complete. To block until the operation is complete, use one of the Connect method overloads, or EndConnect.

For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously


If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.


If this socket has previously been disconnected, then BeginConnect must be called on a thread that will not exit until the operation is complete. This is a limitation of the underlying provider. Also the EndPoint that is used must be different.


This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.


The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

The following code example initiates an asynchronous connection attempt.

public static ManualResetEvent allDone = 
    new ManualResetEvent(false);

// handles the completion of the prior asynchronous 
// connect call. the socket is passed via the objectState 
// paramater of BeginConnect().
public static void ConnectCallback1(IAsyncResult ar)
    Socket s = (Socket) ar.AsyncState;

// Asynchronous connect using the host name, resolved via 
// IPAddress
public static void BeginConnect1(string host, int port)

    IPAddress[] IPs = Dns.GetHostAddresses(host);

    Socket s = new Socket(AddressFamily.InterNetwork,


    Console.WriteLine("Establishing Connection to {0}", 
    s.BeginConnect(IPs[0], port, 
        new AsyncCallback(ConnectCallback1), s);

    // wait here until the connect finishes.  
    // The callback sets allDone.

    Console.WriteLine("Connection established");


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0