Refactored network connection code

CAP Negotiation and connection registration have been moved into
seperate functions, begin_cap() and begin_registration()