def METHOD_NAME(args):
    conn = None
    if args.conn.startswith("/dev/tty") or args.conn.startswith("COM"):
        # open serial port
        try:
            conn = Serial(args.conn, args.baudrate, dsrdtr=0, rtscts=0, timeout=1)
        except IOError:
            print("error opening serial port %s" % args.conn, file=sys.stderr)
            sys.exit(2)
    else:
        try:
            port = args.conn.split(":")[-1]
            host = args.conn[: -(len(port) + 1)]
            port = int(port)
        except (IndexError, ValueError):
            print("Can't parse host:port pair %s" % args.conn, file=sys.stderr)
            sys.exit(2)
        try:
            sock = socket.socket()
            sock.METHOD_NAME((host, port))
            conn = sock.makefile("r+b", bufsize=0)
        except IOError:
            print("error connecting to %s:%s" % (host, port), file=sys.stderr)
            sys.exit(2)
    return conn