internal static void DemandClientConnectionPermissions(ServiceEndpoint endpoint)
        {
            PermissionSet connectionSet = new PermissionSet(PermissionState.None);

            if (endpoint.Binding is NetTcpBinding)
            {
                connectionSet.AddPermission(new SocketPermission(NetworkAccess.Connect, TransportType.Tcp, endpoint.Address.Uri.Host, endpoint.Address.Uri.Port));
                connectionSet.AddPermission(new DnsPermission(PermissionState.Unrestricted));
            }
            if (endpoint.Binding is WebHttpBinding || endpoint.Binding is WSHttpBinding || endpoint.Binding is BasicHttpBinding || endpoint.Binding is WSDualHttpBinding)
            {
                connectionSet.AddPermission(new WebPermission(NetworkAccess.Connect, endpoint.Address.Uri.AbsoluteUri));
            }
            //On the client, demand hosting permission for duplex over HTTP
            if (endpoint.Binding is WSDualHttpBinding)
            {
                connectionSet.AddPermission(new AspNetHostingPermission(AspNetHostingPermissionLevel.Minimal));

                WSDualHttpBinding binding = endpoint.Binding as WSDualHttpBinding;

                Uri callbackUri = binding.ClientBaseAddress ?? new Uri("http://localhost:80/");

                connectionSet.AddPermission(new WebPermission(NetworkAccess.Accept, callbackUri.AbsoluteUri));
            }
            if (endpoint.Binding is NetMsmqBinding)
            {
                string path = QueuedServiceHelper.GetQueueFromUri(endpoint.Address.Uri);
                connectionSet.AddPermission(new MessageQueuePermission(MessageQueuePermissionAccess.Send, path));
            }
            connectionSet.Demand();
        }
        internal static void DemandHostConnectionPermissions(ServiceEndpoint endpoint)
        {
            PermissionSet connectionSet = new PermissionSet(PermissionState.None);

            if (endpoint.Binding is NetTcpBinding)
            {
                connectionSet.AddPermission(new SocketPermission(NetworkAccess.Accept, TransportType.Tcp, endpoint.Address.Uri.Host, endpoint.Address.Uri.Port));
            }
            if (endpoint.Binding is WebHttpBinding || endpoint.Binding is WSHttpBinding || endpoint.Binding is BasicHttpBinding || endpoint.Binding is WSDualHttpBinding)
            {
                connectionSet.AddPermission(new WebPermission(NetworkAccess.Accept, endpoint.Address.Uri.AbsoluteUri));
            }
            if (endpoint.Binding is NetMsmqBinding)
            {
                string path = QueuedServiceHelper.GetQueueFromUri(endpoint.Address.Uri);
                connectionSet.AddPermission(new MessageQueuePermission(MessageQueuePermissionAccess.Receive, path));
            }
            connectionSet.Demand();
        }