try to fix race condition causing exit code 227

Whenever I tried to run nailgun, I would always see intermittent socket disconnects causing the client to return error code 227 before it had finished printing everything to stdout/stderr. After I made this modification to the code, those went away. I believe what is happening is that socket.close does not wait for all the data to be sent to the client before breaking the connection.