import socket
import logging

logger = logging.getLogger(__name__)


class MaxCubeConnection:
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.socket = None
        self.response = None

    def connect(self):
        logger.debug('Connecting to Max! Cube at ' + self.host + ':' + str(self.port))
        if self.socket:
            self.disconnect()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.settimeout(2)
        self.socket.connect((self.host, self.port))
        self.read()

    def read(self):
        buffer_size = 4096
        buffer = bytearray([])
        more = True

        while more:
            try:
                tmp = self.socket.recv(buffer_size)
                more = len(tmp) > 0
                buffer += tmp
            except socket.timeout:
                break
        self.response = buffer.decode('utf-8')

    def send(self, command):
        self.socket.send(command.encode('utf-8'))
        self.read()

    def disconnect(self):
        if self.socket:
            self.send('q:\r\n')
            self.socket.close()
        self.socket = None

@@ -332,32 +332,49 @@
 str(self.port))%0A
+        try:%0A    
         if self.
@@ -389,24 +389,28 @@
             
+  
+  
 self.disconn
@@ -414,16 +414,142 @@
 onnect()
+%0A        except:%0A            logger.debug('Tried disconnecting from cube, caught Exception probably due to stale connection.')
 %0A%0A      

Fix connection.connect fails if connection is existing but broken