'Set port and baudrate if given, then connect to printer'

@locked
def connect(self, port=None, baud=None, dtr=None):

    if self.printer:
        self.disconnect()
    if (port is not None):
        self.port = port
    if (baud is not None):
        self.baud = baud
    if (dtr is not None):
        self.dtr = dtr
    if ((self.port is not None) and (self.baud is not None)):
        host_regexp = re.compile('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$')
        is_serial = True
        if (':' in port):
            bits = port.split(':')
            if (len(bits) == 2):
                hostname = bits[0]
                try:
                    port = int(bits[1])
                    if (host_regexp.match(hostname) and (1 <= port <= 65535)):
                        is_serial = False
                except:
                    pass
        self.writefailures = 0
        if (not is_serial):
            self.printer_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.printer_tcp.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            self.timeout = 0.25
            self.printer_tcp.settimeout(1.0)
            try:
                self.printer_tcp.connect((hostname, port))
                self.printer_tcp.settimeout(self.timeout)
                self.printer = self.printer_tcp.makefile()
            except socket.error as e:
                if (e.strerror is None):
                    e.strerror = ''
                self.logError((((((_('Could    not    connect    to    %s:%s:') % (hostname, port)) + '\n') + (_('Socket    error    %s:') % e.errno)) + '\n') + e.strerror))
                self.printer = None
                self.printer_tcp = None
                return
        else:
            disable_hup(self.port)
            self.printer_tcp = None
            try:
                self.printer = Serial(port=self.port, baudrate=self.baud, timeout=0.25, parity=PARITY_ODD)
                self.printer.close()
                self.printer.parity = PARITY_NONE
                try:
                    self.printer.setDTR(dtr)
                except:
                    pass
                self.printer.open()
            except SerialException as e:
                self.logError((((_('Could    not    connect    to    %s    at    baudrate    %s:') % (self.port, self.baud)) + '\n') + (_('Serial    error:    %s') % e)))
                self.printer = None
                return
            except IOError as e:
                self.logError((((_('Could    not    connect    to    %s    at    baudrate    %s:') % (self.port, self.baud)) + '\n') + (_('IO    error:    %s') % e)))
                self.printer = None
                return
        for handler in self.event_handler:
            try:
                handler.on_connect()
            except:
                logging.error(traceback.format_exc())
        self.stop_read_thread = False
        self.read_thread = threading.Thread(target=self._listen)
        self.read_thread.start()
        self._start_sender()
