Snap a mesh socket to another socket

How i can make a mesh socket to snap to another mesh socket? It’s a bit hard to explain for me but look the picture.

I’m doing this on the fly off the top of my head so there might be errors, but something like this:

FTransform SocketTrans= MyActorB->GetMesh()->GetSocketTransform() * MyActorB->GetTransform().Inverse();
MyActorB->AttachRootComponentTo(/*ActorA*/GetMesh(), "ActorASocket", EAttachLocation::SnapToTarget, true);
FRotator DesiredLocalRotation(0.0f, 0.0f, 180.0f); // any initial rotation you want
FVector NewOfs = DesiredLocalRotation.RotateVector(SocketTrans.GetLocation());
MyActorB->SetActorRelativeRotation(DesiredLocalRotation);
MyActorB->SetActorRelativeLocation(NewOfs);

Note in your example you rotated ‘B’ 180 degrees, so I added that into my code, but it can be any rotation.