Refactored socket code

Started to refactor socket code. This will create a more "modularised"
approach and make things easier and simpler to extend code