modified
fix to wait until socket connection has closed