diff --git a/DTLSSocket/dtls.pyx b/DTLSSocket/dtls.pyx
index 813060c1eb0d67424198c2935e88a2135521a120..013e866fd47d173d8f299844541430c424215276 100644
--- a/DTLSSocket/dtls.pyx
+++ b/DTLSSocket/dtls.pyx
@@ -242,7 +242,9 @@ cdef class DTLS:
   #dtls_peer_t *dtls_get_peer(const dtls_context_t *context, const session_t *session);
   #void dtls_reset_peer(dtls_context_t *ctx, dtls_peer_t *peer)
   def resetPeer(self, Session session: Session):
-    tdtls.dtls_reset_peer(self.ctx, tdtls.dtls_get_peer(self.ctx, session.getSession()))
+    peer = tdtls.dtls_get_peer(self.ctx, &session.session)
+    if peer:
+      tdtls.dtls_reset_peer(self.ctx, peer)
 
   #int dtls_write(dtls_context_t *ctx, session_t *session, uint8 *buf, size_t len)
   def write(self, Session remote: Session, data: bytes):