make sure data is written before closing socket