How do I detect when the preview window / application closes?

I have a socket connection with the server that is called on Begin Play.
Now, when I close the preview window, the editor hands that socket connection.

I can kill it forcefully with:

Socket->Close();
 ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(Socket);

But the problem is that I can only call this before I would have connected again.

So I need a place that detects when the application closes so I can clean up that socket connection.
Rama’s tutorial here Rama’s socket tutorial instructs me to add this at ShutdownModule but I think this is a plugin thing. I want it in my C++ / Blueprint hybrid project, somewhere. Ideally to kill all sockets so I don’t have to pass it through but the socket is static so I can get it easily.