# Buggy code
def __init__(
    self,
    url: str,
    logger: Logger,
    proxy: Optional[str] = None,
    proxy_headers: Optional[Dict[str, str]] = None,
    ping_interval: float = 10,  # seconds
    receive_timeout: float = 5,
    receive_buffer_size: int = 1024,
    trace_enabled: bool = False,
    all_message_trace_enabled: bool = False,
    ping_pong_trace_enabled: bool = False,
    on_message_listener: Optional[Callable[[str], None]] = None,
    on_error_listener: Optional[Callable[[Exception], None]] = None,
    on_close_listener: Optional[Callable[[int, Optional[str]], None]] = None,
    connection_type_name: str = "Socket Mode",
):
    self.url = url
    self.logger = logger
    self.proxy = proxy
    self.proxy_headers = proxy_headers

    self.ping_interval = ping_interval
    self.receive_timeout = receive_timeout
    self.receive_buffer_size = receive_buffer_size
    if self.receive_buffer_size < 16:
        raise SlackClientConfigurationError("Too small receive_buffer_size detected.")

    self.session_id = str(uuid4())
    self.trace_enabled = trace_enabled
    self.all_message_trace_enabled = all_message_trace_enabled
    self.ping_pong_trace_enabled = ping_pong_trace_enabled
    self.last_ping_pong_time = None
    self.consecutive_check_state_error_count = 0
    self.sock = None

    self.on_message_listener = on_message_listener
    self.on_error_listener = on_error_listener
    self.on_close_listener = on_close_listener
    self.connection_type_name = connection_type_name


# 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,
    url: str,
    logger: Logger,
    proxy: Optional[str] = None,
    proxy_headers: Optional[Dict[str, str]] = None,
    ping_interval: float = 5,  # seconds
    receive_timeout: float = 3,
    receive_buffer_size: int = 1024,
    trace_enabled: bool = False,
    all_message_trace_enabled: bool = False,
    ping_pong_trace_enabled: bool = False,
    on_message_listener: Optional[Callable[[str], None]] = None,
    on_error_listener: Optional[Callable[[Exception], None]] = None,
    on_close_listener: Optional[Callable[[int, Optional[str]], None]] = None,
    connection_type_name: str = "Socket Mode",
):
    self.url = url
    self.logger = logger
    self.proxy = proxy
    self.proxy_headers = proxy_headers

    self.ping_interval = ping_interval
    self.receive_timeout = receive_timeout
    self.receive_buffer_size = receive_buffer_size
    if self.receive_buffer_size < 16:
        raise SlackClientConfigurationError("Too small receive_buffer_size detected.")

    self.session_id = str(uuid4())
    self.trace_enabled = trace_enabled
    self.all_message_trace_enabled = all_message_trace_enabled
    self.ping_pong_trace_enabled = ping_pong_trace_enabled
    self.last_ping_pong_time = None
    self.consecutive_check_state_error_count = 0
    self.sock = None
    # To avoid ssl.SSLError: [SSL: BAD_LENGTH] bad length
    self.sock_receive_lock = Lock()
    self.sock_send_lock = Lock()

    self.on_message_listener = on_message_listener
    self.on_error_listener = on_error_listener
    self.on_close_listener = on_close_listener
    self.connection_type_name = connection_type_name