{"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":"Error","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/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/dgram.ts#L596-L632","documentation":"Socket.connect() requires the socket to be in the CONNECT_STATE_DISCONNECTED state; if a previous connect is still in flight (CONNECTING) or already established (CONNECTED), a second connect throws ERR_SOCKET_DGRAM_IS_CONNECTED. A UDP socket has at most one connected peer at a time.","triggerScenarios":"s.connect(port, host) called twice without disconnect(); reconnect logic that connects to a new peer without disconnecting first; racing concurrent connect calls (the second sees CONNECTING and throws).","commonSituations":"Reconnect wrappers copied from TCP code; connecting in a loop on timeout; multiple modules each connecting the same shared socket.","solutions":["Call disconnect() before connecting to a new peer","Track connected state in app code and skip redundant connects","Use a fresh socket per peer, or use send() with explicit addresses for multiple peers"],"exampleFix":"// before\nsock.connect(PORT, HOST);\nsock.connect(PORT2, HOST2); // throws\n// after\nsock.disconnect();\nsock.connect(PORT2, HOST2);","handlingStrategy":"validation","validationCode":"function connectTo(sock, port, host) {\n  try { sock.remoteAddress(); sock.disconnect(); } catch { /* not connected */ }\n  sock.connect(port, host);\n}","typeGuard":null,"tryCatchPattern":"try { sock.connect(port, host); }\ncatch (e) {\n  if (e.code === \"ERR_SOCKET_DGRAM_IS_CONNECTED\") {\n    sock.disconnect();\n    sock.connect(port, host);\n  } else throw e;\n}","preventionTips":["disconnect() before connecting to a new peer","Keep one owner for socket connection state","Use send() with explicit addresses instead of connect when talking to many peers"],"tags":["dgram","udp","connect","state-machine"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}