Replaced close() with shutdown()
Replaced usleep with a check.