Big cleanup. Handle signals to terminate connections more cleanly. Reopen pipein to make sure we do not get stuck in a
POLLHUP loop.