bugfix: moved socket creation outside of the loop