public static void WrappedConnect(this ISocket socket, EndPoint remoteEP) { var invalidateInStoreProxy = false; try { socket.Connect(remoteEP); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.ConnectionReset || ex.SocketErrorCode == SocketError.TimedOut) { invalidateInStoreProxy = true; } throw ex; } catch (Exception ex) when(ex is ProxyException || ex is ProtocolViolationException) { invalidateInStoreProxy = true; throw ex; } finally { if (invalidateInStoreProxy) { socket.InvalidateInStoreProxy(); } } }