<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def scan(host, port=80, url=None, https=False, timeout=1, max_size=65535):
 """ Scan a network port Parameters host : str Host or ip address to scan port : int, optional Port to scan, default=80 url : str, optional URL to perform get request to on the host and port specified https : bool, optional Perform ssl connection on the socket, default=False timeout : float Timeout for network operations, default=1 Returns ------- dict Result dictionary that contains the following keys: host - The host or IP address that was scanned port - The port number that was scanned state - The state of the port, will be either "open" or "closed" durations - An ordered dictionary with floating point value of the time elapsed for each connection operation Raises ------ ScanFailed - The scan operation failed """

    starts = OrderedDict()
    ends = OrderedDict()
    port = int(port)
    result = dict(
        host=host, port=port, state='closed', durations=OrderedDict()
    )
    if url:
        timeout = 1
        result['code'] = None

    starts['all'] = starts['dns'] = datetime.datetime.now()

    # DNS Lookup
    try:
        hostip = socket.gethostbyname(host)
        result['ip'] = hostip
        ends['dns'] = datetime.datetime.now()
    except socket.gaierror:
        raise ScanFailed('DNS Lookup failed', result=result)

    # TCP Connect
    starts['connect'] = datetime.datetime.now()
    network_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    network_socket.settimeout(timeout)
    result_connection = network_socket.connect_ex((hostip, port))
    ends['connect'] = datetime.datetime.now()

    # SSL
    if https:
        starts['ssl'] = datetime.datetime.now()
        try:
            network_socket = ssl.wrap_socket(network_socket)
        except socket.timeout:
            raise ScanFailed('SSL socket timeout', result=result)
        ends['ssl'] = datetime.datetime.now()

    # Get request
    if result_connection == 0 and url:
        starts['request'] = datetime.datetime.now()
        network_socket.send(
            "GET {0} HTTP/1.0\r\nHost: {1}\r\n\r\n".format(
                url, host
            ).encode('ascii'))
        if max_size:
            data = network_socket.recv(max_size)
        else:
            data = network_socket.recv()
        result['length'] = len(data)
        data = data.decode('ascii', errors='ignore')
        result['response'] = (data)
        try:
            result['code'] = int(data.split('\n')[0].split()[1])
        except IndexError:
            pass
        ends['request'] = datetime.datetime.now()
    network_socket.close()

    # Calculate durations
    ends['all'] = datetime.datetime.now()
    for duration in starts.keys():
        if duration in ends.keys():
            result['durations'][duration] = ends[duration] - starts[duration]
    if result_connection == 0:
        result['state'] = 'open'
    return result