Moved connection to its own method

Created a separate ConnectionHandling class with Connect() and CloseConnection() methods so that they could be used everywhere instead of duplicating this code in every functional method.