To outline the problem: I have a socket server listening in a seperate thread to the main program. The socket accepts new connections and deals with them, then listens again. It is in a while loop.
When I click on the button to shut down the socket server, I can not figure out a way to close the thread with the listening socket in it, because it is stuck on the line where it listens. Only after I open a connection and it runs though the code again and realises that run is set to false does the thread close.
This is obviously a problem as when I click shutdown server I want the server to shut down now, not after the next connection!
rough outline:
Code:
(inside the thread)
while 1
{
    socket->listen
    (deal with connection)
    if run is false return 0
}
I am using winsock and windows CreateThread if that is important.

So, does anyone know a way to either run the listening socket differently, or shut down the thread somehow? I wanted to avoid TerminateThread as it doesn't clear everything.

Thanks a lot in advance for all your help.
esaptonor