Added a wait to prevent premature closing of socket