I have a List of TCP sockets I write data to. If the writing fails, I remove it from the list and just carry on.

At least thats the plan. What happens is, that when a client disconnects, the SocketException escalates and the program crashes, even though that exception is handled. The code is below:

```
// sockets is type List<Socket>
foreach (Socket s in sockets)
{
  String jsonString = skeleton.Marshall();
  byte[] jsonBytes = System.Text.Encoding.UTF8.GetBytes(jsonString);

  try
  {
    s.Send(jsonBytes); // boom! System.Net.Sockets.SocketException!
  }
  catch (System.Net.Sockets.SocketException except)
  {
    sockets.Remove(s);
    Console.WriteLine(except.StackTrace);
  }
  catch (Exception except)
  {
    Console.WriteLine(except.StackTrace);
  }
}

```

I don't get why any exception could go through this. I didn't look at the console output because Visual Studio clears that when an exception occurs (at least I didn't see anything meaningful over there)

Thanks for your help!

### Edit

As Sebastian Negraszus pointed out, I can't directly remove the Socket from the List, so the code now is

```
List<Socket> remove = new List<Socket>();

// sockets still is of type List<Socket>
foreach (Socket s in sockets)
{
    String jsonString = skeleton.Marshall();
    byte[] jsonBytes = System.Text.Encoding.UTF8.GetBytes(jsonString);

    try
    {
        s.Send(jsonBytes);
    }
    catch (System.Net.Sockets.SocketException except)
    {
        remove.Add(s);
        Console.WriteLine(except.StackTrace);
    }
    catch (Exception except)
    {
        Console.WriteLine(except.StackTrace);
    }
}

foreach (Socket s in remove)
{
    sockets.Remove(s);
}

```

However, even if the Socket is not removed from the list, it should just escalate here.

### Edit 2

This code runs in an event handler, while sockets is being filled in the main Thread, so I assumed the lack of locking caused problems. However, after adding locks, the error still appeared.

main thread:

```
// ...
sockets = new List<Socket>();
delegateFoo += handlerFunction;
// ...
TcpListener tcpListener = new TcpListener(IPAddress.Any, 20001);
tcpListener.Start();

while (true) {
    Socket s = tcpListener.AcceptSocket();
    lock (sockets) {
        sockets.Add(s);
    }
}

```

handler function:

```
// ...generate skeleton...
lock (sockets)
{
    foreach (Socket s in sockets)
    {
        String jsonString = skeleton.Marshall();
        byte[] jsonBytes = System.Text.Encoding.UTF8.GetBytes(jsonString);

        try
        {
            s.Send(jsonBytes);
        }
        catch (System.Net.Sockets.SocketException except)
        {
            remove.Add(s);
            Console.WriteLine(except.StackTrace);
        }
        catch (Exception except)
        {
            Console.WriteLine(except.StackTrace);
        }
    }

    foreach (Socket s in remove)
    {
        sockets.Remove(s);
    }
}

```

Bad luck though, the Exception still escalates (at least I think so, the program interrupts in VS and this little window occurs saying "SocketException occured" (I use the German version, so the wording might be different).
The error can be triggered by connecting twice using putty and closing one of the two puttys. The next time Send() is called - boom.

### Edit 3: Exception details

I'm sorry these are in German. Translations:

* "... ist aufgetreten" = "... occured"
* "bei" = "at"
* message = "An existing connection has been aborted/terminated by the host computer"

```
System.Net.Sockets.SocketException ist aufgetreten.
  _HResult=-2147467259
  _message=Eine bestehende Verbindung wurde softwaregesteuert durch den Hostcomputer abgebrochen
  HResult=-2147467259
  IsTransient=false
  Message=Eine bestehende Verbindung wurde softwaregesteuert durch den Hostcomputer abgebrochen
  Source=System
  ErrorCode=10053
  NativeErrorCode=10053
  StackTrace:
   bei System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
  InnerException: 

```

And yes, I only Send() once in my program.

### Edit 4: Visual Studio weirdness

Okay, it's Visual Studio being weird. I can uncheck the "break on Exceptions of this type" checkbox and then it just continues. So the exception didn't escalate, but nevertheless made the program stop.

I don't get why you would want to break on handled exceptions by default. I figured that if I uncheck that the program just faults. If you have a better solution, I'd be glad to accept your answer.