<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "Tor",
  "identifier" : "/documentation/Tor/TorControlClient/init(socket:dataDirectory:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Tor"
    ],
    "preciseIdentifier" : "s:3Tor0A13ControlClientC6socket13dataDirectoryAcA0B6SocketC_SSSgtcfc"
  },
  "title" : "init(socket:dataDirectory:)"
}
-->

# init(socket:dataDirectory:)

Wrap an existing [`ControlSocket`](/documentation/Tor/ControlSocket).

```
init(socket: ControlSocket, dataDirectory: String? = nil)
```

## Parameters

`socket`

The underlying transport. Ownership is shared;
the client does not close the socket on `deinit`.

`dataDirectory`

Optional path to Tor’s data directory. When
non-`nil`, [`authenticate(password:)`](/documentation/Tor/TorControlClient/authenticate(password:)) will try cookie
authentication by reading `<dataDir>/control_auth_cookie`.

## Discussion

The primary low-level initialiser. Use when the caller already
has a [`ControlSocket`](/documentation/Tor/ControlSocket) in hand — typically because they
constructed one from a TCP endpoint or are injecting a mock for
tests. The socket may be freshly-connected or already
authenticated externally.

> Note: Freshly constructed instances have
> ``doc://Tor/documentation/Tor/TorControlClient/isAuthenticated`` = `false`. Callers must explicitly invoke
> ``doc://Tor/documentation/Tor/TorControlClient/authenticate(password:)`` before issuing any other command.