Add blocking UDP connection and refactor some code