libmemif: refactor connection establishment