protected virtual OperationResponse ConnectAndAuthenticate(UnifiedTestClient client, string address,
                                                                   string userName, Dictionary <byte, object> authParameter = null, bool reuseToken = false, short expectedErrorCode = 0)
        {
            if (client.Connected)
            {
                client.Disconnect();
            }

            if (!reuseToken &&
                address == this.MasterAddress
#pragma warning disable CS0618 // Type or member is obsolete
                && this.authPolicy == AuthPolicy.AuthOnMaster)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                client.Token = string.Empty;
            }

            client.OperationResponseQueueClear();
            client.EventQueueClear();

            if (authParameter == null)
            {
                authParameter = new Dictionary <byte, object>();
            }

            if (this.authPolicy != AuthPolicy.UseAuthOnce ||
                this.NameServerAddress == address)
            {
                ConnectToServer(client, address);
                if (this.authPolicy != AuthPolicy.UseAuthOnce)
                {
                    return(client.Authenticate(userName, authParameter, expectedErrorCode));
                }

                return(client.AuthOnce(userName, authParameter, expectedErrorCode));
            }

            client.ConnectWithAuthOnce(address, expectedErrorCode);
            return(null);
        }