fixed bug in connection algorithm