public void Listen()
        {
            // If you call listen() on a port, then kill the process, then immediately start a new process and 
            // try to listen() on the same port, you sometimes get WSAEADDRINUSE.  Even if nothing was accepted.  
            // Ports don't immediately free themselves on process shutdown.  We call listen() in a loop on a delay 
            // for a few iterations for this reason. 
            //
            TimeSpan listenTimeout = TimeSpan.FromSeconds(1);
            BackoffTimeoutHelper backoffHelper = new BackoffTimeoutHelper(listenTimeout);

            lock (ThisLock)
            {
                if (this.listenSocket != null)
                {
                    this.listenSocket.Listen(settings.ListenBacklog);
                    isListening = true;
                }

                while (!isListening)
                {
                    try
                    {
                        this.listenSocket = new Socket(localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                        if (localEndpoint.AddressFamily == AddressFamily.InterNetworkV6 && settings.TeredoEnabled)
                        {
                            this.listenSocket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)23, 10);
                        }

                        this.listenSocket.Bind(localEndpoint);
                        this.listenSocket.Listen(settings.ListenBacklog);
                        isListening = true;
                    }
                    catch (SocketException socketException)
                    {
                        bool retry = false;

                        if (socketException.ErrorCode == UnsafeNativeMethods.WSAEADDRINUSE)
                        {
                            if (!backoffHelper.IsExpired())
                            {
                                backoffHelper.WaitAndBackoff();
                                retry = true;
                            }
                        }

                        if (!retry)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                SocketConnectionListener.ConvertListenException(socketException, this.localEndpoint));
                        }
                    }
                }

                this.socketAsyncEventArgsPool = new SocketAsyncEventArgsPool(GetAcceptBufferSize(this.listenSocket));
            }
        }
 private ServiceControllerStatus ExitServiceStatus(ServiceController service, int pollMin, int pollMax, ServiceControllerStatus status)
 {
     ServiceControllerStatus status2;
     BackoffTimeoutHelper helper = new BackoffTimeoutHelper(TimeSpan.MaxValue, TimeSpan.FromMilliseconds((double) pollMax), TimeSpan.FromMilliseconds((double) pollMin));
     do
     {
         if (this.closed)
         {
             return service.Status;
         }
         helper.WaitAndBackoff();
         service.Refresh();
         status2 = service.Status;
     }
     while (status2 == status);
     return status2;
 }
 public void Listen()
 {
     BackoffTimeoutHelper helper = new BackoffTimeoutHelper(TimeSpan.FromSeconds(1.0));
     lock (this.ThisLock)
     {
         if (this.listenSocket != null)
         {
             this.listenSocket.Listen(this.settings.ListenBacklog);
             this.isListening = true;
         }
         while (!this.isListening)
         {
             try
             {
                 this.listenSocket = new Socket(this.localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                 if ((this.localEndpoint.AddressFamily == AddressFamily.InterNetworkV6) && this.settings.TeredoEnabled)
                 {
                     this.listenSocket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPProtectionLevel, 10);
                 }
                 this.listenSocket.Bind(this.localEndpoint);
                 this.listenSocket.Listen(this.settings.ListenBacklog);
                 this.isListening = true;
                 continue;
             }
             catch (SocketException exception)
             {
                 bool flag = false;
                 if ((exception.ErrorCode == 0x2740) && !helper.IsExpired())
                 {
                     helper.WaitAndBackoff();
                     flag = true;
                 }
                 if (!flag)
                 {
                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertListenException(exception, this.localEndpoint));
                 }
                 continue;
             }
         }
         this.socketAsyncEventArgsPool = new SocketAsyncEventArgsPool(GetAcceptBufferSize(this.listenSocket));
     }
 }
 private void ReconnectCallback(object state)
 {
     BackoffTimeoutHelper helper = new BackoffTimeoutHelper(TimeSpan.MaxValue, TimeSpan.FromMinutes(5.0), TimeSpan.FromSeconds(30.0));
     while (this.state == CommunicationState.Opening)
     {
         try
         {
             this.StartListen(true);
         }
         catch (Exception exception)
         {
             if (Fx.IsFatal(exception))
             {
                 throw;
             }
             if (DiagnosticUtility.ShouldTraceError)
             {
                 DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Error);
             }
         }
         if (this.state == CommunicationState.Opening)
         {
             helper.WaitAndBackoff();
         }
     }
 }