Update connection.js

Changed loop structure