Implemented clean server socket termination.

The server had a thread that accepts new connections, and which was
blocking on accept. This was replaced by a select() over the socket and
a pipe, which allows the thread to be stopped whenever requested.