<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "Event",
  "identifier" : "/documentation/Event/Socket/connect(to:loop:timeout:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Event"
    ],
    "preciseIdentifier" : "s:5Event6SocketC7connect2to4loop7timeoutAcA0B7AddressV_AA0A4LoopCs8DurationVSgtYaKFZ"
  },
  "title" : "connect(to:loop:timeout:)"
}
-->

# connect(to:loop:timeout:)

Connects to a remote [`SocketAddress`](/documentation/Event/SocketAddress).

```
static func connect(to address: SocketAddress, loop: EventLoop = .shared, timeout: Duration? = nil) async throws -> Socket
```

## Parameters

`address`

A pre-built endpoint from [`SocketAddress`](/documentation/Event/SocketAddress).

`loop`

The event loop to drive. Defaults to [`shared`](/documentation/Event/EventLoop/shared).

`timeout`

Maximum time to wait for handshake completion. `nil` (default)
waits indefinitely. On expiry the call throws [`SocketError.timeout`](/documentation/Event/SocketError/timeout);
the partially-connected descriptor is closed before the error is thrown.

## Return Value

A connected `Socket`.

## Discussion

Creates a fresh non-blocking TCP socket, calls `connect(2)`, and (for
`EINPROGRESS`) registers an `EV_WRITE` event to resume the continuation when
the kernel completes the handshake — bounded by `timeout` if provided.

> Throws: ``doc://Event/documentation/Event/SocketError/socketCreationFailed(_:)`` if `socket(2)` fails;
> ``doc://Event/documentation/Event/SocketError/connectionFailed(_:)`` if `connect(2)` fails with anything
> other than `EINPROGRESS`; ``doc://Event/documentation/Event/SocketError/timeout`` if `timeout` elapses
> before the kernel reports write-readiness.