Get hostname and connect to server

Added the code to get server's hostname and then connect to server. Also
added code for error handling.