> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PartyManager::ConnectToNetwork

> Queues an asynchronous attempt to connect to a network.

Queues an asynchronous attempt to connect to a network.

## Syntax

```cpp theme={null}
PartyError ConnectToNetwork(  
    const PartyNetworkDescriptor* networkDescriptor,  
    void* asyncIdentifier,  
    PartyNetwork** network  
)  
```

### Parameters

**`networkDescriptor`**   [PartyNetworkDescriptor\*](/services/playfab/multiplayer/networking/reference/structs/partynetworkdescriptor)

The descriptor associated with the network to which a connection will be established.

**`asyncIdentifier`**   void\*\
*optional*

An optional, app-defined, pointer-sized context value that can be used to associate the completion state change with this call.

**`network`**   [PartyNetwork\*\*](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/partynetwork)\
*optional, library-allocated output*

The optional, output network object.

### Return value

PartyError

`c_partyErrorSuccess` if the asynchronous operation to connect to a new network began, or an error code otherwise. If this method fails, no related state changes will be generated. The human-readable form of the error code can be retrieved via [GetErrorMessage()](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_geterrormessage).

## Remarks

This method queues an asynchronous attempt to connect to the network associated with `networkDescriptor` by negotiating and establishing a secure DTLS connection from the local device to the transparent cloud relay server. A [PartyConnectToNetworkCompletedStateChange](/services/playfab/multiplayer/networking/reference/structs/partyconnecttonetworkcompletedstatechange) will be provided upon completion of the asynchronous operation, indicating success or failure. On success, the local device will have established a connection to the transparent cloud relay server. On failure, a [PartyNetworkDestroyedStateChange](/services/playfab/multiplayer/networking/reference/structs/partynetworkdestroyedstatechange) will be generated. No other devices will become visible, and the local device won't be visible to any remote devices, until at least one local user is successfully authenticated via [PartyNetwork::AuthenticateLocalUser()](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_authenticatelocaluser). <br /><br /> After the device successfully connects to the network, it must authenticate into the network via PartyNetwork::AuthenticateLocalUser(). If the device is connected to the network but unauthenticated for more than one minute, the device will be disconnected and a PartyNetworkDestroyedStateChange will be generated. This will occur regardless of whether the device never authenticated or lost authentication after calling [PartyNetwork::RemoveLocalUser()](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_removelocaluser).   <br /><br /> This method optionally provides `network` as output that can immediately be used to perform asynchronous network operations, such as [PartyNetwork::CreateInvitation()](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_createinvitation) and [PartyNetwork::CreateEndpoint()](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_createendpoint). These asynchronous operations will be internally queued until the connection completes, at which point they'll be processed. This `network` will also be provided on the resulting PartyConnectToNetworkCompletedStateChange where it will be fully connected and associated with the provided `asyncIdentifier`.   <br /><br /> This operation will asynchronously fail with [PartyConnectToNetworkCompletedStateChange::result](/services/playfab/multiplayer/networking/reference/structs/partyconnecttonetworkcompletedstatechange) set to [PartyStateChangeResult::NetworkLimitReached](/services/playfab/multiplayer/networking/reference/enums/partystatechangeresult) if the limit of connected devices for the network has been reached as specified in the network's configuration when it was created via [CreateNewNetwork()](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_createnewnetwork).   <br /><br /> On successful return, this method invalidates the memory for any array previously returned by [GetNetworks()](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_getnetworks), as it synchronously adds the new network to the array. [StartProcessingStateChanges()](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_startprocessingstatechanges) also invalidates the memory for the array. The returned `network` object will be valid until a PartyNetworkDestroyedStateChange has been generated and all state changes referencing the object have been returned to [FinishProcessingStateChanges()](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_finishprocessingstatechanges).

### Retrying on failure

When ConnectToNetwork() fails asynchronously, a PartyConnectToNetworkCompletedStateChange is provided by StartProcessingStateChanges() indicating the result. The operation may be retried after a delay, depending on the [PartyStateChangeResult](/services/playfab/multiplayer/networking/reference/enums/partystatechangeresult) provided.   <br />

| Result                       | Retry behavior                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| InternetConnectivityError    | Retry with a small delay of no less than 10 seconds. For your app, it may be more appropriate to display the error to the user immediately, rather than retrying automatically.                                                                                                                                                                                                                                                                                          |
| NetworkLimitReached          | Don't retry automatically. Instead, display a message to the user and wait for the user to initiate another attempt.                                                                                                                                                                                                                                                                                                                                                     |
| NetworkNoLongerExists        | Don't retry.                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| VersionMismatch              | Don't retry.                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| FailedToBindToLocalUdpSocket | This result means that the library couldn't bind to the local UDP socket specified in the [PartyOption::LocalUdpSocketBindAddress](/services/playfab/multiplayer/networking/reference/enums/partyoption) option. The title must clean up its instance of the library, update the [PartyOption::LocalUdpSocketBindAddress](/services/playfab/multiplayer/networking/reference/enums/partyoption) option to a valid, available bind address, and reinitialize the library. |

## Requirements

**Header:** Party.h

## See also

[PartyManager](/services/playfab/multiplayer/networking/reference/classes/PartyManager/partymanager)\
[PartyConnectToNetworkCompletedStateChange](/services/playfab/multiplayer/networking/reference/structs/partyconnecttonetworkcompletedstatechange)\
[PartyNetworkDestroyedStateChange](/services/playfab/multiplayer/networking/reference/structs/partynetworkdestroyedstatechange)\
[PartyNetworkConfiguration](/services/playfab/multiplayer/networking/reference/structs/partynetworkconfiguration)\
[PartyManager::CreateNewNetwork](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_createnewnetwork)\
[PartyManager::GetNetworks](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_getnetworks)\
[PartyNetwork::AuthenticateLocalUser](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_authenticatelocaluser)\
[PartyNetwork::RemoveLocalUser](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_removelocaluser)\
[PartyNetwork::CreateInvitation](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_createinvitation)\
[PartyNetwork::RevokeInvitation](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_revokeinvitation)\
[PartyNetwork::CreateEndpoint](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_createendpoint)\
[PartyNetwork::LeaveNetwork](/services/playfab/multiplayer/networking/reference/classes/PartyNetwork/methods/partynetwork_leavenetwork)


## Related topics

- [PartyManager::GetNetworks](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_getnetworks.md)
- [PartyManager::DeserializeNetworkDescriptor](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_deserializenetworkdescriptor.md)
- [PartyManager::SerializeNetworkDescriptor](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_serializenetworkdescriptor.md)
- [PartyManager::CreateNewNetwork](/services/playfab/multiplayer/networking/reference/classes/PartyManager/methods/partymanager_createnewnetwork.md)
- [PartyConnectToNetworkCompletedStateChange](/services/playfab/multiplayer/networking/reference/structs/partyconnecttonetworkcompletedstatechange.md)
