private string GenerateConnectionStringForNewRunspace()
        {
            string str = null;
            WSManConnectionInfo connectionInfo = this.remoteRunspaceInfo.Runspace.ConnectionInfo as WSManConnectionInfo;

            if (connectionInfo == null)
            {
                return(str);
            }
            if (connectionInfo.UseDefaultWSManPort)
            {
                bool flag;
                WSManConnectionInfo.GetConnectionString(connectionInfo.ConnectionUri, out flag);
                return(string.Format(CultureInfo.InvariantCulture, "-ComputerName '{0}' `\r\n                    -ApplicationName '{1}' {2} {3} ", new object[] { CommandMetadata.EscapeSingleQuotedString(connectionInfo.ComputerName), CommandMetadata.EscapeSingleQuotedString(connectionInfo.AppName), connectionInfo.UseDefaultWSManPort ? string.Empty : string.Format(CultureInfo.InvariantCulture, "-Port {0} ", new object[] { connectionInfo.Port }), flag ? "-useSSL" : string.Empty }));
            }
            return(string.Format(CultureInfo.InvariantCulture, "-connectionUri '{0}'", new object[] { CommandMetadata.EscapeSingleQuotedString(this.GetConnectionString()) }));
        }
        private void Initialize(Uri connectionUri, WSManConnectionInfo connectionInfo)
        {
            bool   isSSLSpecified = false;
            string str            = connectionUri.OriginalString;

            if (connectionUri == connectionInfo.ConnectionUri && connectionInfo.UseDefaultWSManPort)
            {
                str = WSManConnectionInfo.GetConnectionString(connectionInfo.ConnectionUri, out isSSLSpecified);
            }
            string connection;

            if (string.IsNullOrEmpty(connectionUri.Query))
            {
                connection = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}?PSVersion={1}", (object)str.TrimEnd('/'), (object)PSVersionInfo.PSVersion);
            }
            else
            {
                connection = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0};PSVersion={1}", (object)str, (object)PSVersionInfo.PSVersion);
            }
            WSManNativeApi.BaseWSManAuthenticationCredentials authenticationCredentials;
            if (connectionInfo.CertificateThumbprint != null)
            {
                authenticationCredentials = (WSManNativeApi.BaseWSManAuthenticationCredentials) new WSManNativeApi.WSManCertificateThumbprintCredentials(connectionInfo.CertificateThumbprint);
            }
            else
            {
                string       name = (string)null;
                SecureString pwd  = (SecureString)null;
                if (connectionInfo.Credential != null && !string.IsNullOrEmpty(connectionInfo.Credential.UserName))
                {
                    name = connectionInfo.Credential.UserName;
                    pwd  = connectionInfo.Credential.Password;
                }
                authenticationCredentials = (WSManNativeApi.BaseWSManAuthenticationCredentials) new WSManNativeApi.WSManUserNameAuthenticationCredentials(name, pwd, connectionInfo.WSManAuthenticationMechanism);
            }
            WSManNativeApi.WSManUserNameAuthenticationCredentials authCredentials = (WSManNativeApi.WSManUserNameAuthenticationCredentials)null;
            if (connectionInfo.ProxyCredential != null)
            {
                WSManNativeApi.WSManAuthenticationMechanism authMechanism = WSManNativeApi.WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_NEGOTIATE;
                string       name = (string)null;
                SecureString pwd  = (SecureString)null;
                switch (connectionInfo.ProxyAuthentication)
                {
                case AuthenticationMechanism.Basic:
                    authMechanism = WSManNativeApi.WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_BASIC;
                    break;

                case AuthenticationMechanism.Negotiate:
                    authMechanism = WSManNativeApi.WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_NEGOTIATE;
                    break;

                case AuthenticationMechanism.Digest:
                    authMechanism = WSManNativeApi.WSManAuthenticationMechanism.WSMAN_FLAG_AUTH_DIGEST;
                    break;
                }
                if (!string.IsNullOrEmpty(connectionInfo.ProxyCredential.UserName))
                {
                    name = connectionInfo.ProxyCredential.UserName;
                    pwd  = connectionInfo.ProxyCredential.Password;
                }
                authCredentials = new WSManNativeApi.WSManUserNameAuthenticationCredentials(name, pwd, authMechanism);
            }
            WSManNativeApi.WSManProxyInfo wsManProxyInfo = connectionInfo.ProxyAccessType == ProxyAccessType.None ? (WSManNativeApi.WSManProxyInfo)null : new WSManNativeApi.WSManProxyInfo(connectionInfo.ProxyAccessType, authCredentials);
            int errorCode = 0;

            try
            {
                errorCode = WSManNativeApi.WSManCreateSession(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, connection, 0, (IntPtr)authenticationCredentials.GetMarshalledObject(), wsManProxyInfo == null ? IntPtr.Zero : (IntPtr)wsManProxyInfo, ref this.wsManSessionHandle);
            }
            finally
            {
                authCredentials?.Dispose();
                wsManProxyInfo?.Dispose();
                authenticationCredentials?.Dispose();
            }
            if (errorCode != 0)
            {
                throw new PSInvalidOperationException(WSManNativeApi.WSManGetErrorMessage(WSManClientSessionTransportManager.wsManApiStaticData.WSManAPIHandle, errorCode));
            }
            int num;

            WSManNativeApi.WSManGetSessionOptionAsDword(this.wsManSessionHandle, WSManNativeApi.WSManSessionOption.WSMAN_OPTION_SHELL_MAX_DATA_SIZE_PER_MESSAGE_KB, out num);
            this.Fragmentor.FragmentSize = num << 10;
            this.dataToBeSent.Fragmentor = this.Fragmentor;
            this.noCompression           = !connectionInfo.UseCompression;
            this.noMachineProfile        = connectionInfo.NoMachineProfile;
            this.idleTimeout             = (long)connectionInfo.IdleTimeout;
            if (isSSLSpecified)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_USE_SSL, 1);
            }
            if (connectionInfo.NoEncryption)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_UNENCRYPTED_MESSAGES, 1);
            }
            if (connectionInfo.AllowImplicitCredentialForNegotiate)
            {
                WSManNativeApi.WSManSetSessionOption(this.wsManSessionHandle, WSManNativeApi.WSManSessionOption.WSMAN_OPTION_ALLOW_NEGOTIATE_IMPLICIT_CREDENTIALS, new WSManNativeApi.WSManDataDWord(1));
            }
            if (connectionInfo.UseUTF16)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_UTF16, 1);
            }
            if (connectionInfo.SkipCACheck)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_SKIP_CA_CHECK, 1);
            }
            if (connectionInfo.SkipCNCheck)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_SKIP_CN_CHECK, 1);
            }
            if (connectionInfo.SkipRevocationCheck)
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_SKIP_REVOCATION_CHECK, 1);
            }
            string name1 = connectionInfo.UICulture.Name;

            if (!string.IsNullOrEmpty(name1))
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_UI_LANGUAGE, name1);
            }
            string name2 = connectionInfo.Culture.Name;

            if (!string.IsNullOrEmpty(name2))
            {
                this.SetWSManSessionOption(WSManNativeApi.WSManSessionOption.WSMAN_OPTION_LOCALE, name2);
            }
            this.SetDefaultTimeOut(connectionInfo.OperationTimeout);
            this.SetConnectTimeOut(connectionInfo.OpenTimeout);
            this.SetCloseTimeOut(connectionInfo.CancelTimeout);
            this.SetSignalTimeOut(connectionInfo.CancelTimeout);
        }