Refine connection logic