<!--
{
  "availability" : [
    "macOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Virtualization",
  "identifier" : "/documentation/Virtualization/VZVirtioSocketDevice/connect(toPort:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Virtualization"
    ],
    "preciseIdentifier" : "s:So20VZVirtioSocketDeviceC14VirtualizationE7connect6toPort17completionHandlerys6UInt32V_ys6ResultOySo0aB10ConnectionCs5Error_pGctF"
  },
  "title" : "connect(toPort:completionHandler:)"
}
-->

# connect(toPort:completionHandler:)

Initiates a connection to the specified port of the guest operating system.

```
func connect(toPort port: UInt32, completionHandler: @escaping (Result<VZVirtioSocketConnection, any Error>) -> Void)
```

## Parameters

`port`

The destination port number in the guest operating system.

`completionHandler`

The block to execute with the results of the connection attempt. This block has no return value and takes the following parameter:

- result: The result of the connection attempt. On a successful attempt, this value is the [`VZVirtioSocketConnection`](/documentation/Virtualization/VZVirtioSocketConnection) object to use for communications. On an unsuccessful attempt, this value is the error object that indicates why the connection failed.

## Discussion

This method initiates the connection asynchronously, and executes the completion handler when the results are available. If the guest operating system doesn’t listen for connections to the specifed port, this method does nothing.

For a successful connection, this method sets the [`sourcePort`](/documentation/Virtualization/VZVirtioSocketConnection/sourcePort) property of the resulting [`VZVirtioSocketConnection`](/documentation/Virtualization/VZVirtioSocketConnection) object to a random port number.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)