private const int TimeoutInSecs = 180; // Timeout value for service connection

        internal static void CreateConnectionToService(string cmHostname, int cmServicePortno, bool sslEnabled, string cmServiceUsername, string cmServicePassword, int timeout = TimeoutInSecs)
        {
            try
            {
                WebHttpBinding bd = new WebHttpBinding();
                bd.UseDefaultWebProxy = false;
                bd.SendTimeout        = TimeSpan.FromSeconds(timeout);
                bd.ReceiveTimeout     = TimeSpan.FromSeconds(timeout);

                bd.MaxBufferSize          = 1024 * 1024 * 10;
                bd.MaxReceivedMessageSize = 10 * 1024 * 1024;
                bd.MaxBufferPoolSize      = 10 * 1024 * 1024;
                bd.ReaderQuotas.MaxStringContentLength = 128 * 1024;

                if (!ValidateHostNameAndPort(cmHostname, cmServicePortno))
                {
                    return;
                }

                // If enable encryption is true
                if (sslEnabled)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
                    bd.Security.Mode = WebHttpSecurityMode.Transport;
                    bd.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
                    String uri = "https://" + cmHostname + ":" + cmServicePortno;
                    serviceChannel = new ChannelFactory <Contracts.IChassisManager>(bd, uri);
                }
                else
                {
                    bd.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
                    bd.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
                    String uri = "http://" + cmHostname + ":" + cmServicePortno;
                    serviceChannel = new ChannelFactory <Contracts.IChassisManager>(bd, uri);
                }

                serviceChannel.Endpoint.Behaviors.Add(new WebHttpBehavior());

                // Check if user credentials are specified, if not use default
                if (cmServiceUsername != null && cmServicePassword != null)
                {
                    // Set user credentials specified
                    serviceChannel.Credentials.Windows.ClientCredential =
                        new System.Net.NetworkCredential(cmServiceUsername, cmServicePassword);
                }

                channel = serviceChannel.CreateChannel();
            }
            catch (Exception)
            {
                //Exception occured when creating service channel with given config params, return false
                Console.WriteLine("failed to connect to service");
            }
        }
        private const int TimeoutInSecs = 180; // Timeout value for service connection

        internal static void CreateConnectionToService(string cmHostname, int cmServicePortno, bool sslEnabled, string cmServiceUsername, string cmServicePassword, int timeout = TimeoutInSecs)
        {
            try
            {
                WebHttpBinding bd = new WebHttpBinding();
                bd.UseDefaultWebProxy = false;
                bd.SendTimeout = TimeSpan.FromSeconds(timeout);
                bd.ReceiveTimeout = TimeSpan.FromSeconds(timeout);

                bd.MaxBufferSize = 1024 * 1024 * 10;
                bd.MaxReceivedMessageSize = 10 * 1024 * 1024;
                bd.MaxBufferPoolSize = 10 * 1024 * 1024;
                bd.ReaderQuotas.MaxStringContentLength = 128 * 1024;

                if (!ValidateHostNameAndPort(cmHostname, cmServicePortno))
                    return;

                // If enable encryption is true
                if (sslEnabled)
                {
                    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
                    bd.Security.Mode = WebHttpSecurityMode.Transport;
                    bd.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
                    String uri = "https://" + cmHostname + ":" + cmServicePortno;
                    serviceChannel = new ChannelFactory<Contracts.IChassisManager>(bd, uri);
                }
                else
                {
                    bd.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
                    bd.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
                    String uri = "http://" + cmHostname + ":" + cmServicePortno;
                    serviceChannel = new ChannelFactory<Contracts.IChassisManager>(bd, uri);
                }

                serviceChannel.Endpoint.Behaviors.Add(new WebHttpBehavior());

                // Check if user credentials are specified, if not use default
                if (cmServiceUsername!=null && cmServicePassword!=null)
                {
                    // Set user credentials specified
                    serviceChannel.Credentials.Windows.ClientCredential =
                            new System.Net.NetworkCredential(cmServiceUsername, cmServicePassword);
                } 

                channel = serviceChannel.CreateChannel();
            }
            catch (Exception)
            {
                //Exception occured when creating service channel with given config params, return false
                Console.WriteLine("failed to connect to service");
            }
        }