'Socket maintenance.'

def maintain_sockets(self):

    while (not self.socket_queue.empty()):
        (server, socket) = self.socket_queue.get()
        if (server in self.connecting):
            self.connecting.remove(server)
        if socket:
            self.new_interface(server, socket)
        else:
            self.connection_down(server)
    for interface in self.interfaces.values():
        if interface.has_timed_out():
            self.connection_down(interface.server)
        elif interface.ping_required():
            params = [ELECTRUM_VERSION, PROTOCOL_VERSION]
            self.queue_request('server.version', params, interface)
    now = time.time()
    if ((len(self.interfaces) + len(self.connecting)) < self.num_server):
        self.start_random_interface()
        if ((now - self.nodes_retry_time) > NODES_RETRY_INTERVAL):
            self.print_error('network:    retrying    connections')
            self.disconnected_servers = set([])
            self.nodes_retry_time = now
    if (not self.is_connected()):
        if self.auto_connect:
            if (not self.is_connecting()):
                self.switch_to_random_interface()
        elif (self.default_server in self.disconnected_servers):
            if ((now - self.server_retry_time) > SERVER_RETRY_INTERVAL):
                self.disconnected_servers.remove(self.default_server)
                self.server_retry_time = now
        else:
            self.switch_to_interface(self.default_server)
