def connect_APRS(sock):
            #    
            #-----------------------------------------------------------------
            #
            # This function tries to shutdown the specified sock and if it
            # fails closes it and then creates a new one and reconnects to the APRS system
            #    
            #-----------------------------------------------------------------
            #
            try:
                sock.shutdown(0)
            except socket.error, e:
                if 'not connected' in e:
                    print '*** Transport endpoint is not connected ***'
                print "socket no longer open so can't be closed, create new one"    
            else:
                print "Socket still open so close it"
                sock.close()
            print "Create new socket"
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                sock.connect((settings.APRS_SERVER_HOST, settings.APRS_SERVER_PORT))
            except Exception, e:
                print "Connection refused. Errno: ", e
                exit() 
            APRSparm = ('user %s pass %s vers %s %s filter r/%s/%s/%s\n ' % (settings.APRS_USER, 
                                                                             settings.APRS_PASSCODE, 
                                                                             settings.FLOGGER_NAME, 
                                                                             settings.FLOGGER_VER, 
                                                                             settings.FLOGGER_LATITUDE, 
                                                                             settings.FLOGGER_LONGITUDE, 
                                                                             settings.FLOGGER_RAD))