# Buggy code
def __init__(
    self,
    app_token: str,
    logger: Optional[Logger] = None,
    web_client: Optional[WebClient] = None,
    auto_reconnect_enabled: bool = True,
    trace_enabled: bool = False,
    all_message_trace_enabled: bool = False,
    ping_pong_trace_enabled: bool = False,
    ping_interval: float = 5,
    receive_buffer_size: int = 1024,
    concurrency: int = 10,
    proxy: Optional[str] = None,
    proxy_headers: Optional[Dict[str, str]] = None,
    on_message_listeners: Optional[List[Callable[[str], None]]] = None,
    on_error_listeners: Optional[List[Callable[[Exception], None]]] = None,
    on_close_listeners: Optional[List[Callable[[int, Optional[str]], None]]] = None,
):
    self.app_token = app_token
    self.logger = logger or logging.getLogger(__name__)
    self.web_client = web_client or WebClient()
    self.default_auto_reconnect_enabled = auto_reconnect_enabled
    self.auto_reconnect_enabled = self.default_auto_reconnect_enabled
    self.trace_enabled = trace_enabled
    self.all_message_trace_enabled = all_message_trace_enabled
    self.ping_pong_trace_enabled = ping_pong_trace_enabled
    self.ping_interval = ping_interval
    self.receive_buffer_size = receive_buffer_size
    if self.receive_buffer_size < 16:
        raise SlackClientConfigurationError("Too small receive_buffer_size detected.")

    self.wss_uri = None
    self.message_queue = Queue()
    self.message_listeners = []
    self.socket_mode_request_listeners = []

    self.current_session = None
    self.current_session_state = ConnectionState()
    self.current_session_runner = IntervalRunner(self._run_current_session, 0.5).start()

    self.current_app_monitor_started = False
    self.current_app_monitor = IntervalRunner(
        self._monitor_current_session, self.ping_interval
    )

    self.closed = False
    self.connect_operation_lock = Lock()

    self.message_processor = IntervalRunner(self.process_messages, 0.001).start()
    self.message_workers = ThreadPoolExecutor(max_workers=concurrency)

    self.proxy = proxy
    if self.proxy is None or len(self.proxy.strip()) == 0:
        env_variable = load_http_proxy_from_env(self.logger)
        if env_variable is not None:
            self.proxy = env_variable
    self.proxy_headers = proxy_headers

    self.on_message_listeners = on_message_listeners or []
    self.on_error_listeners = on_error_listeners or []
    self.on_close_listeners = on_close_listeners or []


# Traceback (SlackApiError)
2021-02-14 18:19:49,579 ERROR    slack_sdk.rtm.v2          Failed to establish a connection (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81, error: _ssl.c:1108: The handshake operation timed out)
Traceback (most recent call last):
File "/opt/errbot/lib/python3.8/site-packages/slack_sdk/socket_mode/builtin/connection.py", line 109, in connect
sock.connect((hostname, port))
File "/usr/lib/python3.8/ssl.py", line 1342, in connect
self._real_connect(addr, False)
File "/usr/lib/python3.8/ssl.py", line 1333, in _real_connect
self.do_handshake()
File "/usr/lib/python3.8/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
socket.timeout: _ssl.c:1108: The handshake operation timed out
2021-02-14 18:19:49,579 INFO     slack_sdk.rtm.v2          The connection has been closed (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:19:49,579 INFO     slack_sdk.rtm.v2          The connection has been closed (session id: b88f63ee-d644-45a8-bf18-be36c8673728)
2021-02-14 18:19:49,579 INFO     slack_sdk.rtm.v2          A new session has been established (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:19:49,579 INFO     slack_sdk.rtm.v2          Connected to a new endpoint...
2021-02-14 18:19:59,590 DEBUG    slack_sdk.rtm.v2          This connection is already closed. (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:19:59,590 INFO     slack_sdk.rtm.v2          The session seems to be already closed. Going to reconnect... (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:19:59,590 INFO     slack_sdk.rtm.v2          Connecting to a new endpoint...
2021-02-14 18:20:00,075 ERROR    slack_sdk.rtm.v2          Failed to retrieve WSS URL: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'ratelimited'}
2021-02-14 18:20:00,075 ERROR    slack_sdk.rtm.v2          Failed to check the current session or reconnect to the server (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81, error: SlackApiError, message: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'ratelimited'})
2021-02-14 18:20:10,092 DEBUG    slack_sdk.rtm.v2          This connection is already closed. (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:20:10,117 INFO     slack_sdk.rtm.v2          The session seems to be already closed. Going to reconnect... (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:20:10,121 INFO     slack_sdk.rtm.v2          Connecting to a new endpoint...
2021-02-14 18:20:10,339 ERROR    slack_sdk.rtm.v2          Failed to retrieve WSS URL: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'ratelimited'}
2021-02-14 18:20:10,339 ERROR    slack_sdk.rtm.v2          Failed to check the current session or reconnect to the server (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81, error: SlackApiError, message: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'ratelimited'})
2021-02-14 18:20:20,350 DEBUG    slack_sdk.rtm.v2          This connection is already closed. (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:20:20,350 INFO     slack_sdk.rtm.v2          The session seems to be already closed. Going to reconnect... (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:20:20,350 INFO     slack_sdk.rtm.v2          Connecting to a new endpoint...
2021-02-14 18:20:20,556 ERROR    slack_sdk.rtm.v2          Failed to retrieve WSS URL: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'ratelimited'}
2021-02-14 18:20:20,557 ERROR    slack_sdk.rtm.v2          Failed to check the current session or reconnect to the server (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81, error: SlackApiError, message: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'ratelimited'})
2021-02-14 18:20:30,567 DEBUG    slack_sdk.rtm.v2          This connection is already closed. (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:20:30,567 INFO     slack_sdk.rtm.v2          The session seems to be already closed. Going to reconnect... (session id: 18e8f520-5e64-443c-98ed-089ab1ef6f81)
2021-02-14 18:20:30,567 INFO     slack_sdk.rtm.v2          Connecting to a new endpoint...

# Fixed code
def __init__(
    self,
    app_token: str,
    logger: Optional[Logger] = None,
    web_client: Optional[WebClient] = None,
    auto_reconnect_enabled: bool = True,
    trace_enabled: bool = False,
    all_message_trace_enabled: bool = False,
    ping_pong_trace_enabled: bool = False,
    ping_interval: float = 5,
    receive_buffer_size: int = 1024,
    concurrency: int = 10,
    proxy: Optional[str] = None,
    proxy_headers: Optional[Dict[str, str]] = None,
    on_message_listeners: Optional[List[Callable[[str], None]]] = None,
    on_error_listeners: Optional[List[Callable[[Exception], None]]] = None,
    on_close_listeners: Optional[List[Callable[[int, Optional[str]], None]]] = None,
):
    self.app_token = app_token
    self.logger = logger or logging.getLogger(__name__)
    self.web_client = web_client or WebClient()
    self.default_auto_reconnect_enabled = auto_reconnect_enabled
    self.auto_reconnect_enabled = self.default_auto_reconnect_enabled
    self.trace_enabled = trace_enabled
    self.all_message_trace_enabled = all_message_trace_enabled
    self.ping_pong_trace_enabled = ping_pong_trace_enabled
    self.ping_interval = ping_interval
    self.receive_buffer_size = receive_buffer_size
    if self.receive_buffer_size < 16:
        raise SlackClientConfigurationError("Too small receive_buffer_size detected.")

    self.wss_uri = None
    self.message_queue = Queue()
    self.message_listeners = []
    self.socket_mode_request_listeners = []

    self.current_session = None
    self.current_session_state = ConnectionState()
    self.current_session_runner = IntervalRunner(self._run_current_session, 0.1).start()

    self.current_app_monitor_started = False
    self.current_app_monitor = IntervalRunner(
        self._monitor_current_session, self.ping_interval
    )

    self.closed = False
    self.connect_operation_lock = Lock()

    self.message_processor = IntervalRunner(self.process_messages, 0.001).start()
    self.message_workers = ThreadPoolExecutor(max_workers=concurrency)

    self.proxy = proxy
    if self.proxy is None or len(self.proxy.strip()) == 0:
        env_variable = load_http_proxy_from_env(self.logger)
        if env_variable is not None:
            self.proxy = env_variable
    self.proxy_headers = proxy_headers

    self.on_message_listeners = on_message_listeners or []
    self.on_error_listeners = on_error_listeners or []
    self.on_close_listeners = on_close_listeners or []