{"record":{"id":"18791d3be80b1bbb","repo":"TooTallNate/Java-WebSocket","slug":"socket-has-already-been-set","errorCode":null,"errorMessage":"socket has already been set","messagePattern":"socket has already been set","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/client/WebSocketClient.java","lineNumber":905,"sourceCode":"   */\n  public void setProxy(Proxy proxy) {\n    if (proxy == null) {\n      throw new IllegalArgumentException();\n    }\n    this.proxy = proxy;\n  }\n\n  /**\n   * Accepts bound and unbound sockets.<br> This method must be called before <code>connect</code>.\n   * If the given socket is not yet bound it will be bound to the uri specified in the constructor.\n   *\n   * @param socket The socket which should be used for the connection\n   * @deprecated use setSocketFactory\n   */\n  @Deprecated\n  public void setSocket(Socket socket) {\n    if (this.socket != null) {\n      throw new IllegalStateException(\"socket has already been set\");\n    }\n    this.socket = socket;\n  }\n\n  /**\n   * Accepts a SocketFactory.<br> This method must be called before <code>connect</code>. The socket\n   * will be bound to the uri specified in the constructor.\n   *\n   * @param socketFactory The socket factory which should be used for the connection.\n   */\n  public void setSocketFactory(SocketFactory socketFactory) {\n    this.socketFactory = socketFactory;\n  }\n\n  @Override\n  public void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin) {\n    engine.sendFragmentedFrame(op, buffer, fin);\n  }","sourceCodeStart":887,"sourceCodeEnd":923,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/client/WebSocketClient.java#L887-L923","documentation":"This is a defensive state guard in the deprecated WebSocketClient.setSocket method: it fires when the client's internal socket field has already been assigned and setSocket is called a second time. The WebSocket library allows configuring the underlying socket only once, before connect(); re-invoking setSocket would silently replace an existing socket and corrupt connection state, so the IllegalStateException aborts the configuration. The faulty input is a second setSocket call on the same client instance (typically by application code that retries configuration or mixes deprecated setSocket with new-style setup).","triggerScenarios":"Thrown at src/main/java/org/java_websocket/client/WebSocketClient.java:905 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Call setSocket at most once per WebSocketClient instance, before connect()","Remove duplicate initialization code paths that both configure the socket (e.g. both setSocket and setSocketFactory usage/retry)","Create a new WebSocketClient instance instead of re-configuring an existing one","Migrate to setSocketFactory, which is the non-deprecated replacement and does not carry this one-shot restriction"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d","analyzedAt":"2026-09-09T14:39:47.546Z","contentChangedAt":"2026-09-09T14:39:47.546Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}