# Buggy code
def __init__(
    self,
    host,
    user=None,
    password=None,
    port=None,
    pkey=None,
    num_retries=DEFAULT_RETRIES,
    retry_delay=RETRY_DELAY,
    allow_agent=True,
    timeout=None,
    forward_ssh_agent=False,
    proxy_host=None,
    proxy_port=None,
    proxy_pkey=None,
    proxy_user=None,
    proxy_password=None,
    _auth_thread_pool=True,
    keepalive_seconds=60,
    identity_auth=True,
):
    """:param host: Host name or IP to connect to.
    :type host: str
    :param user: User to connect as. Defaults to logged in user.
    :type user: str
    :param password: Password to use for password authentication.
    :type password: str
    :param port: SSH port to connect to. Defaults to SSH default (22)
    :type port: int
    :param pkey: Private key file path to use for authentication. Path must
      be either absolute path or relative to user home directory
      like ``~/<path>``.
    :type pkey: str
    :param num_retries: (Optional) Number of connection and authentication
      attempts before the client gives up. Defaults to 3.
    :type num_retries: int
    :param retry_delay: Number of seconds to wait between retries. Defaults
      to :py:class:`pssh.constants.RETRY_DELAY`
    :type retry_delay: int
    :param timeout: SSH session timeout setting in seconds. This controls
      timeout setting of authenticated SSH sessions.
    :type timeout: int
    :param allow_agent: (Optional) set to False to disable connecting to
      the system's SSH agent
    :type allow_agent: bool
    :param identity_auth: (Optional) set to False to disable attempting to
      authenticate with default identity files from
      `pssh.clients.base_ssh_client.BaseSSHClient.IDENTITIES`
    :type identity_auth: bool
    :param forward_ssh_agent: (Optional) Turn on SSH agent forwarding -
      equivalent to `ssh -A` from the `ssh` command line utility.
      Defaults to True if not set.
    :type forward_ssh_agent: bool
    :param proxy_host: Connect to target host via given proxy host.
    :type proxy_host: str
    :param proxy_port: Port to use for proxy connection. Defaults to self.port
    :type proxy_port: int
    :param keepalive_seconds: Interval of keep alive messages being sent to
      server. Set to ``0`` or ``False`` to disable.

    :raises: :py:class:`pssh.exceptions.PKeyFileError` on errors finding
      provided private key.
    """
    self.forward_ssh_agent = forward_ssh_agent
    self._forward_requested = False
    self.keepalive_seconds = keepalive_seconds
    self._keepalive_greenlet = None
    self._proxy_client = None
    self.host = host
    self.port = port
    if proxy_host is not None:
        _port = port if proxy_port is None else proxy_port
        _pkey = pkey if proxy_pkey is None else proxy_pkey
        _user = user if proxy_user is None else proxy_user
        _password = password if proxy_password is None else proxy_password
        proxy_port = self._connect_proxy(
            proxy_host,
            _port,
            _pkey,
            user=_user,
            password=_password,
            num_retries=num_retries,
            retry_delay=retry_delay,
            allow_agent=allow_agent,
            timeout=timeout,
            keepalive_seconds=keepalive_seconds,
            identity_auth=identity_auth,
        )
        proxy_host = "127.0.0.1"
    super(SSHClient, self).__init__(
        host,
        user=user,
        password=password,
        port=port,
        pkey=pkey,
        num_retries=num_retries,
        retry_delay=retry_delay,
        allow_agent=allow_agent,
        _auth_thread_pool=_auth_thread_pool,
        timeout=timeout,
        proxy_host=proxy_host,
        proxy_port=proxy_port,
        identity_auth=identity_auth,
    )


# Traceback (ssh2.exceptions.SocketRecvError)
Traceback (most recent call last):
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 194, in _init_session
THREAD_POOL.apply(self.session.handshake, (self.sock,))
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\pool.py", line 161, in apply
return self.spawn(func, *args, **kwds).get()
File "src\\gevent\\event.py", line 305, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 335, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 323, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 303, in gevent._gevent_cevent.AsyncResult._raise_exception
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\_compat.py", line 65, in reraise
raise value.with_traceback(tb)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\threadpool.py", line 142, in __run_task
thread_result.set(func(*args, **kwargs))
File "ssh2\session.pyx", line 138, in ssh2.session.Session.handshake
File "ssh2\utils.pyx", line 214, in ssh2.utils.handle_error_codes
ssh2.exceptions.SocketRecvError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 194, in _init_session
THREAD_POOL.apply(self.session.handshake, (self.sock,))
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\pool.py", line 161, in apply
return self.spawn(func, *args, **kwds).get()
File "src\\gevent\\event.py", line 305, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 335, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 323, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 303, in gevent._gevent_cevent.AsyncResult._raise_exception
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\_compat.py", line 65, in reraise
raise value.with_traceback(tb)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\threadpool.py", line 142, in __run_task
thread_result.set(func(*args, **kwargs))
File "ssh2\session.pyx", line 138, in ssh2.session.Session.handshake
File "ssh2\utils.pyx", line 214, in ssh2.utils.handle_error_codes
ssh2.exceptions.SocketRecvError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Albert\Desktop\mikrotik_test.py", line 63, in <module>
test = execute_command_and_return_output(my_command, host = host, password = "some_password", use_proxy = True)
File "C:\Users\Albert\Desktop\mikrotik_test.py", line 51, in execute_command_and_return_output
client = prepare_connection(host = host, password = password, use_proxy = use_proxy)
File "C:\Users\Albert\Desktop\mikrotik_test.py", line 28, in prepare_connection
client = SSHClient(dev_host, user = user, password = pwd, pkey = pkey, proxy_host=proxy_host, proxy_user=proxy_user, proxy_pkey=proxy_pkey)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 127, in __init__
super(SSHClient, self).__init__(
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\base\single.py", line 90, in __init__
self._init()
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\base\single.py", line 94, in _init
self._init_session()
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 200, in _init_session
return self._connect_init_session_retry(retries=retries+1)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\base\single.py", line 139, in _connect_init_session_retry
return self._init_session(retries=retries)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 200, in _init_session
return self._connect_init_session_retry(retries=retries+1)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\base\single.py", line 139, in _connect_init_session_retry
return self._init_session(retries=retries)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 194, in _init_session
THREAD_POOL.apply(self.session.handshake, (self.sock,))
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\pool.py", line 161, in apply
return self.spawn(func, *args, **kwds).get()
File "src\\gevent\\event.py", line 305, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 335, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 323, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 303, in gevent._gevent_cevent.AsyncResult._raise_exception
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\_compat.py", line 65, in reraise
raise value.with_traceback(tb)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\threadpool.py", line 142, in __run_task
thread_result.set(func(*args, **kwargs))
File "ssh2\session.pyx", line 138, in ssh2.session.Session.handshake
File "ssh2\utils.pyx", line 214, in ssh2.utils.handle_error_codes
ssh2.exceptions.SocketRecvError
[Finished in 28.4s with exit code 1]
[shell_cmd: python -u "C:\Users\Albert\Desktop\mikrotik_test.py"]
[dir: C:\Users\Albert\Desktop]
[path: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\IDEMIA\AWP\Dlls;C:\Program Files\IDEMIA\AWP\Dlls;C:\Program Files (x86)\Google\Google Apps Migration\;C:\Program Files (x86)\Google\Google Apps Sync\;C:\Program Files\Git\cmd;C:\ProgramData\ComposerSetup\bin;C:\Users\Albert\AppData\Local\Programs\Python\Python38\Scripts\;C:\Users\Albert\AppData\Local\Programs\Python\Python38\;C:\Users\Albert\AppData\Local\Microsoft\WindowsApps;C:\Users\Albert\AppData\Local\Programs\Microsoft VS Code\bin;C:\php;C:\Users\Albert\AppData\Roaming\Composer\vendor\bin]

# Fixed code
def __init__(
    self,
    host,
    user=None,
    password=None,
    port=None,
    pkey=None,
    num_retries=DEFAULT_RETRIES,
    retry_delay=RETRY_DELAY,
    allow_agent=True,
    timeout=None,
    forward_ssh_agent=False,
    proxy_host=None,
    proxy_port=None,
    proxy_pkey=None,
    proxy_user=None,
    proxy_password=None,
    _auth_thread_pool=True,
    keepalive_seconds=60,
    identity_auth=True,
):
    """:param host: Host name or IP to connect to.
    :type host: str
    :param user: User to connect as. Defaults to logged in user.
    :type user: str
    :param password: Password to use for password authentication.
    :type password: str
    :param port: SSH port to connect to. Defaults to SSH default (22)
    :type port: int
    :param pkey: Private key file path to use for authentication. Path must
      be either absolute path or relative to user home directory
      like ``~/<path>``.
    :type pkey: str
    :param num_retries: (Optional) Number of connection and authentication
      attempts before the client gives up. Defaults to 3.
    :type num_retries: int
    :param retry_delay: Number of seconds to wait between retries. Defaults
      to :py:class:`pssh.constants.RETRY_DELAY`
    :type retry_delay: int
    :param timeout: SSH session timeout setting in seconds. This controls
      timeout setting of authenticated SSH sessions.
    :type timeout: int
    :param allow_agent: (Optional) set to False to disable connecting to
      the system's SSH agent
    :type allow_agent: bool
    :param identity_auth: (Optional) set to False to disable attempting to
      authenticate with default identity files from
      `pssh.clients.base_ssh_client.BaseSSHClient.IDENTITIES`
    :type identity_auth: bool
    :param forward_ssh_agent: (Optional) Turn on SSH agent forwarding -
      equivalent to `ssh -A` from the `ssh` command line utility.
      Defaults to True if not set.
    :type forward_ssh_agent: bool
    :param proxy_host: Connect to target host via given proxy host.
    :type proxy_host: str
    :param proxy_port: Port to use for proxy connection. Defaults to self.port
    :type proxy_port: int
    :param keepalive_seconds: Interval of keep alive messages being sent to
      server. Set to ``0`` or ``False`` to disable.

    :raises: :py:class:`pssh.exceptions.PKeyFileError` on errors finding
      provided private key.
    """
    self.forward_ssh_agent = forward_ssh_agent
    self._forward_requested = False
    self.keepalive_seconds = keepalive_seconds
    self._keepalive_greenlet = None
    self._proxy_client = None
    self.host = host
    self.port = port if port is not None else 22
    if proxy_host is not None:
        _port = port if proxy_port is None else proxy_port
        _pkey = pkey if proxy_pkey is None else proxy_pkey
        _user = user if proxy_user is None else proxy_user
        _password = password if proxy_password is None else proxy_password
        proxy_port = self._connect_proxy(
            proxy_host,
            _port,
            _pkey,
            user=_user,
            password=_password,
            num_retries=num_retries,
            retry_delay=retry_delay,
            allow_agent=allow_agent,
            timeout=timeout,
            keepalive_seconds=keepalive_seconds,
            identity_auth=identity_auth,
        )
        proxy_host = "127.0.0.1"
    super(SSHClient, self).__init__(
        host,
        user=user,
        password=password,
        port=port,
        pkey=pkey,
        num_retries=num_retries,
        retry_delay=retry_delay,
        allow_agent=allow_agent,
        _auth_thread_pool=_auth_thread_pool,
        timeout=timeout,
        proxy_host=proxy_host,
        proxy_port=proxy_port,
        identity_auth=identity_auth,
    )