<!--
{
  "availability" : [

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

# connect(to:loop:)

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

```
static func connect(to address: SocketAddress, loop: EventLoop = .shared) 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).

## 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.

> 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`.