private TelegramClient SpinUpClient(int dc)
        {
            object dcLock;

            lock (_spinUpLocks)
            {
                if (_spinUpLocks.ContainsKey(dc))
                {
                    dcLock = _spinUpLocks[dc];
                }
                else
                {
                    dcLock           = new object();
                    _spinUpLocks[dc] = dcLock;
                }
            }

            lock (dcLock)
            {
                var client = GetClientInternal(dc);
                if (client != null)
                {
                    return(client);
                }

                if (_config == null)
                {
                    DebugPrint("Config is null. Unable to resolve DC information.");
                    return(null);
                }

                var dcOption = _config.DcOptions.Cast <DcOption>().FirstOrDefault(x => x.Id == dc);

                if (dcOption == null)
                {
                    DebugPrint("Unable to find DC for DC ID: " + dc);
                    return(null);
                }

                var dcCached = DcDatabase.Get(dc);

                var transportConfig =
                    new TcpClientTransportConfig(dcOption.IpAddress, (int)dcOption.Port);

                SharpMTProto.Authentication.AuthInfo authInfo;
                AuthExportedAuthorization            exportedAuth = null;

                if (dcCached == null)
                {
                    DebugPrint("Looks like we'll have to authenticate a new connection to: "
                               + ObjectDumper.Dump(dcOption));

                    DebugPrint(">>>>>>>> Exporting auth...");

                    using (var clientDisposable = new TelegramClientDisposable(this))
                    {
                        exportedAuth = (AuthExportedAuthorization)TelegramUtils.RunSynchronously(clientDisposable.Client.Methods.AuthExportAuthorizationAsync(
                                                                                                     new SharpTelegram.Schema.Layer18.AuthExportAuthorizationArgs
                        {
                            DcId = (uint)dc,
                        }));
                    }

                    DebugPrint(">>>>>>> Got exported auth.");

                    if (exportedAuth == null)
                    {
                        DebugPrint("Exported auth is null for some weird reason. DC ID: " + dc);
                        return(null);
                    }

                    DebugPrint(">>>>>>> Fetching new authentication...");

                    authInfo = TelegramUtils.RunSynchronously(FetchNewAuthentication(transportConfig));
                }
                else
                {
                    authInfo = new SharpMTProto.Authentication.AuthInfo(dcCached.Key,
                                                                        BitConverter.ToUInt64(dcCached.Salt, 0));
                }

                DebugPrint(">>>>>>>> Starting new client...");

                var newClient = new TelegramClient(transportConfig,
                                                   new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo);

                newClient.OnClosedInternally += (sender, e) =>
                {
                    DebugPrint("Removing connection to DC: " + dc);
                    lock (_activeClients)
                    {
                        _activeClients.Remove(dc);
                    }
                };

                var result = TelegramUtils.RunSynchronously(newClient.Connect());
                if (result != MTProtoConnectResult.Success)
                {
                    DebugPrint("Failed to connect to DC: " + dc + ": " + result);
                    return(null);
                }

                if (exportedAuth != null)
                {
                    TelegramUtils.RunSynchronously(newClient.Methods.AuthImportAuthorizationAsync(new AuthImportAuthorizationArgs
                    {
                        Id    = exportedAuth.Id,
                        Bytes = exportedAuth.Bytes,
                    }));
                }

                PingDelay(client, 60);

                lock (_activeClients)
                {
                    _activeClients[dc] = newClient;
                }

                if (dcCached == null)
                {
                    DcDatabase.Set(new TelegramDc
                    {
                        Dc   = dc,
                        Key  = authInfo.AuthKey,
                        Salt = BitConverter.GetBytes(authInfo.Salt),
                    });
                }

                return(newClient);
            }
        }
 public override void OnResponse(BinaryReader reader)
 {
     exportedAuthorization = TLObject.Read <AuthExportedAuthorization>(reader);
 }