connect function

The connect function establishes a connection to a specified socket.


int WSAAPI connect(
  SOCKET         s,
  const sockaddr *name,
  int            namelen



A descriptor identifying an unconnected socket.


A pointer to the sockaddr structure to which the connection should be established.


The length, in bytes, of the sockaddr structure pointed to by the name parameter.

Return Value

If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.





If the error code returned indicates the connection attempt failed (that is, WSAECONNREFUSED, WSAENETUNREACH, WSAETIMEDOUT) the application can call connect again for the same socket.

Error code Meaning
A successful WSAStartup call must occur before using this function.
The network subsystem has failed.
The socket's local address is already in use and the socket was not marked to allow address reuse with SO_REUSEADDR. This error usually occurs when executing bind, but could be delayed until the connect function if the bind was to a wildcard address (INADDR_ANY or in6addr_any) for the local IP address. A specific address needs to be implicitly bound by the connect function.
The blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall.
A nonblocking connect call is in progress on the specified socket.
The remote address is not a valid address (such as INADDR_ANY or in6addr_any) .
Addresses in the specified family cannot be used with this socket.
The attempt to connect was forcefully rejected.
The sockaddr structure pointed to by the name contains incorrect address format for the associated address family or the namelen parameter is too small. This error is also returned if the sockaddr structure pointed to by the name parameter with a length specified in the namelen parameter is not in a valid part of the user address space.
The parameter s is a listening socket.
The network cannot be reached from this host at this time.
A socket operation was attempted to an unreachable host.
Note  No buffer space is available. The socket cannot be connected.
The descriptor specified in the s parameter is not a socket.
An attempt to connect timed out without establishing a connection.
An attempt to connect a datagram socket to broadcast address failed because setsockopt option SO_BROADCAST is not enabled.


The connect function is used to create a connection to the specified destination. If socket s, is unbound, unique values are assigned to the local association by the system, and the socket is marked as bound.

For connection-oriented sockets (for example, type SOCK_STREAM), an active connection is initiated to the foreign host using name (an address in the namespace of the socket; for a detailed description, see bind and sockaddr).



When the socket call completes successfully, the socket is ready to send and receive data. If the address member of the structure specified by the name parameter is filled with zeros, connect will return the error WSAEADDRNOTAVAIL. Any attempt to reconnect an active connection will fail with the error code WSAEISCONN.

For connection-oriented, nonblocking sockets, it is often not possible to complete the connection immediately. In such a case, this function returns the error WSAEWOULDBLOCK. However, the operation proceeds.

When the success or failure outcome becomes known, it may be reported in one of two ways, depending on how the client registers for notification.

  • If the client uses the functions WSAAsyncSelect or WSAEventSelect, the notification is announced with FD_CONNECT and the error code associated with the FD_CONNECT indicates either success or a specific reason for failure.


For connectionless sockets, name can indicate any valid address, including a broadcast address. However, to connect to a broadcast address, a socket must use setsockopt to enable the SO_BROADCAST option. Otherwise, connect will fail with the error code WSAEACCES.

When a connection between sockets is broken, the socket that was connected should be discarded and new socket should be created. When a problem develops on a connected socket, the application must discard the socket and create the socket again in order to return to a stable point.


Example Code

The following example demonstrates the use of the connect function.
#ifndef UNICODE
#define UNICODE


#include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h>

// Need to link with Ws2_32.lib #pragma comment(lib, "ws2_32.lib")

int wmain() { //---------------------- // Initialize Winsock WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != NO_ERROR) { wprintf(L"WSAStartup function failed with error: %d\n", iResult); return 1; } //---------------------- // Create a SOCKET for connecting to server SOCKET ConnectSocket; ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ConnectSocket == INVALID_SOCKET) { wprintf(L"socket function failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } //---------------------- // The sockaddr_in structure specifies the address family, // IP address, and port of the server to be connected to. sockaddr_in clientService; clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr(""); clientService.sin_port = htons(27015);

// Connect to server.
if (iResult == SOCKET_ERROR) {
    wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
    iResult = closesocket(ConnectSocket);
    if (iResult == SOCKET_ERROR)
        wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
    return 1;

wprintf(L"Connected to server.\n");

iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR) {
    return 1;

return 0;


For another example that uses the connect function, see Getting Started With Winsock.

Notes for IrDA Sockets

  • The Af_irda.h header file must be explicitly included.

IrDA implements the connect function with addresses of the form sockaddr_irda. Typically, a client application will create a socket with the socket function, scan the immediate vicinity for IrDA devices with the IRLMP_ENUMDEVICES socket option, choose a device from the returned list, form an address, and then call connect. There is no difference between blocking and nonblocking semantics.


Windows version Windows Vista [desktop apps | UWP apps] Windows Server 2003 [desktop apps | UWP apps]
Target Platform Windows
Header winsock2.h
Library Ws2_32.lib
DLL Ws2_32.dll

See Also



Winsock Functions

Winsock Reference