What do you mean hangs? Does it reach the read loop? Or do you mean that it never exits? One problem could be that your server doesn't close the socket. The read loop won't exit until the socket is closed (and sysread returns zero). One way to check this is put a "print STDERR" inside the loop. Another is to unbuffer output to BIN_FILE and see if anything shows up in it.