{"record":{"id":"0348a5bc2761d1b8","repo":"denoland/deno","slug":"err-socket-dgram-is-connected","errorCode":"ERR_SOCKET_DGRAM_IS_CONNECTED","errorMessage":"Already connected","messagePattern":"Already connected","errorType":"exception","errorClass":"NodeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/dgram.ts","lineNumber":614,"sourceCode":"    callback?: (err?: ErrnoException) => void,\n  ): void;\n  connect(port: number, callback: (err?: ErrnoException) => void): void;\n  connect(port: number, address?: unknown, callback?: unknown) {\n    port = validatePort(port, \"Port\", false);\n\n    if (typeof address === \"function\") {\n      callback = address;\n      address = \"\";\n    } else if (address === undefined) {\n      address = \"\";\n    }\n\n    validateString(address, \"address\");\n\n    const state = this[kStateSymbol];\n\n    if (state.connectState !== CONNECT_STATE_DISCONNECTED) {\n      throw new ERR_SOCKET_DGRAM_IS_CONNECTED();\n    }\n\n    state.connectState = CONNECT_STATE_CONNECTING;\n\n    if (state.bindState === BIND_STATE_UNBOUND) {\n      // deno-lint-ignore deno-internal/prefer-primordials -- Socket's own bind method, not Function.prototype.bind\n      this.bind({ port: 0, exclusive: true });\n    }\n\n    if (state.bindState !== BIND_STATE_BOUND) {\n      enqueue(\n        this,\n        FunctionPrototypeBind(\n          _connect,\n          this,\n          port,\n          address as string,\n          callback as (err?: ErrnoException) => void,","sourceCodeStart":596,"sourceCodeEnd":632,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/dgram.ts#L596-L632","documentation":"Socket.prototype.connect() associates the unconnected socket with one remote address. If the connect state is anything other than DISCONNECTED (already connected, or a connect still pending), it throws ERR_SOCKET_DGRAM_IS_CONNECTED.","triggerScenarios":"Calling connect() twice in a row. A connect() issued while an earlier connect() has not completed. Reconnect logic that calls connect() without disconnect() first.","commonSituations":"Failover code that switches remote endpoints without disconnecting. Event handlers that connect on every message. Two startup paths that both connect the same socket.","solutions":["Call disconnect() before you connect() to a new endpoint.","Track connected state yourself and skip redundant connects.","Chain endpoint changes: disconnect, wait for completion, then connect."],"exampleFix":"// before\nsocket.connect(newPort, newHost); // already connected\n\n// after\nif (connected) socket.disconnect();\nsocket.connect(newPort, newHost);","handlingStrategy":"validation","validationCode":"let connected = false;\nsocket.once('connect', () => { connected = true; });\nfunction reconnect(port: number, host: string) { if (connected) socket.disconnect(); socket.connect(port, host); }","typeGuard":null,"tryCatchPattern":"try { socket.connect(port, host); } catch (e) { if (e.code === 'ERR_SOCKET_DGRAM_IS_CONNECTED') { socket.disconnect(); socket.connect(port, host); } else throw e; }","preventionTips":["Track connect state with a flag set on the 'connect' event.","Always pair endpoint changes with disconnect() first."],"tags":["dgram","udp","connect","socket-state"],"backgroundTag":"already-connected","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}