More cleanup

Fix bug in read loop.

Calculate max fd for select() instead of using FD_SETSIZE.