protected async virtual Task RequestConnectionAsync(NetworkStream nStream, byte command, string destinationHost, int destinationPort, CancellationToken cancellationToken = default) { var dstIp = await HostHelper.GetIPAddressBytesAsync(destinationHost); var dstPort = HostHelper.GetPortBytes(destinationPort); var userId = Array.Empty <byte>(); // Set the credentials if needed if (Settings.Credentials != null && !string.IsNullOrEmpty(Settings.Credentials.UserName)) { userId = Encoding.ASCII.GetBytes(Settings.Credentials.UserName); } // REQUEST GRANT // +----+----+----+----+----+----+----+----+----+----+....+----+ // | VN | CD | DSTPORT | DSTIP | USERID |NULL| // +----+----+----+----+----+----+----+----+----+----+....+----+ // 1 1 2 4 variable 1 var request = new byte[9 + userId.Length]; request[0] = VersionNumber; request[1] = command; dstPort.CopyTo(request, 2); dstIp.CopyTo(request, 4); userId.CopyTo(request, 8); request[8 + userId.Length] = 0x00; await nStream.WriteAsync(request.AsMemory(0, request.Length), cancellationToken).ConfigureAwait(false); // READ RESPONSE // +----+----+----+----+----+----+----+----+ // | VN | CD | DSTPORT | DSTIP | // +----+----+----+----+----+----+----+----+ // 1 1 2 4 var response = new byte[8]; await nStream.ReadAsync(response.AsMemory(0, response.Length), cancellationToken).ConfigureAwait(false); var reply = response[1]; if (reply != CommandReplyRequestGranted) { HandleCommandError(reply); } }