@@ -245,56 +245,46 @@ public void StartListening(bool blocking = false)
245245
246246 private void acceptSocket ( IAsyncResult result , string key )
247247 {
248- #if DEBUG
249- // Workaround for bug in .NET 4.0 and 4.5:
250- // https://connect.microsoft.com/VisualStudio/feedback/details/535917
251- new Thread ( ( ) =>
252- #endif
253- {
254- // Ensure that this callback is really due to a new connection (might be due to listening socket closure)
255- if ( ! IsListening )
256- return ;
248+ // Ensure that this callback is really due to a new connection (might be due to listening socket closure)
249+ if ( ! IsListening )
250+ return ;
257251
258- // Get the socket
259- Socket socket = null ;
260- try { socket = _listeningSockets [ key ] . Socket . EndAccept ( result ) ; }
261- catch ( SocketException ) { } // can happen if the remote party has closed the socket while it was waiting for us to accept
262- catch ( ObjectDisposedException ) { }
263- catch ( NullReferenceException ) { if ( _listeningSockets [ key ] != null ) throw ; } // can happen if StopListening is called at precisely the "wrong" time
252+ // Get the socket
253+ Socket socket = null ;
254+ try { socket = _listeningSockets [ key ] . Socket . EndAccept ( result ) ; }
255+ catch ( SocketException ) { } // can happen if the remote party has closed the socket while it was waiting for us to accept
256+ catch ( ObjectDisposedException ) { }
257+ catch ( NullReferenceException ) { if ( _listeningSockets [ key ] != null ) throw ; } // can happen if StopListening is called at precisely the "wrong" time
264258
265- // Schedule the next socket accept
266- if ( _listeningSockets [ key ] != null )
267- try { _listeningSockets [ key ] . Socket . BeginAccept ( r => acceptSocket ( r , key ) , null ) ; }
268- catch ( NullReferenceException ) { if ( _listeningSockets [ key ] != null ) throw ; } // can happen if StopListening is called at precisely the "wrong" time
259+ // Schedule the next socket accept
260+ if ( _listeningSockets [ key ] != null )
261+ try { _listeningSockets [ key ] . Socket . BeginAccept ( r => acceptSocket ( r , key ) , null ) ; }
262+ catch ( NullReferenceException ) { if ( _listeningSockets [ key ] != null ) throw ; } // can happen if StopListening is called at precisely the "wrong" time
269263
270- // Handle this connection
271- if ( socket == null )
272- return ;
264+ // Handle this connection
265+ if ( socket == null )
266+ return ;
273267
274- if ( PropagateExceptions )
268+ if ( PropagateExceptions )
269+ HandleConnection ( socket , _listeningSockets [ key ] . Secure ) ;
270+ else
271+ {
272+ try
273+ {
275274 HandleConnection ( socket , _listeningSockets [ key ] . Secure ) ;
276- else
275+ }
276+ catch ( Exception e )
277277 {
278+ try { socket . Close ( ) ; }
279+ catch { }
278280 try
279281 {
280- HandleConnection ( socket , _listeningSockets [ key ] . Secure ) ;
281- }
282- catch ( Exception e )
283- {
284- try { socket . Close ( ) ; }
285- catch { }
286- try
287- {
288- _log . Error ( "{0} ({1})" . Fmt ( e . Message , e . GetType ( ) . FullName ) ) ;
289- _log . Error ( e . StackTrace ) ;
290- }
291- catch { }
282+ _log . Error ( "{0} ({1})" . Fmt ( e . Message , e . GetType ( ) . FullName ) ) ;
283+ _log . Error ( e . StackTrace ) ;
292284 }
285+ catch { }
293286 }
294287 }
295- #if DEBUG
296- ) . Start ( ) ;
297- #endif
298288 }
299289
300290 /// <summary>
@@ -510,6 +500,12 @@ private void asyncCallbackTryBlock(IAsyncResult ar, SslStream secureStream)
510500 Socket . Close ( ) ;
511501 return ;
512502 }
503+ catch ( AuthenticationException auth ) when ( auth . Message == "Cannot determine the frame size or a corrupted frame was received." )
504+ {
505+ // Very common exception that appears to be harmless
506+ Socket . Close ( ) ;
507+ return ;
508+ }
513509 catch ( Exception e )
514510 {
515511 Socket . Close ( ) ;
0 commit comments