Refine automatic connection function