How do I get the correct IK transform for a character's hand for holding weapons?

I have a setup that lets me put my character’s left hand on a socket on the gun.

I also have a socket in my character’s left hand. I’d like to put the hand so that the hand socket world transform matches the weapon socket’s world transform.

Basically, for the hand pose, I need to return the position of the hand relative of the socket location in world space.

The code I have below almost works, but my character’s hand is backwards. I’ve tried a few things, but can’t seem to get it right. Can anyone help me with my 3D math?

The code is slightly based off this function. USkinnedMeshComponent::TransformToBoneSpace | Unreal Engine Documentation

void MyCharacter::GetLeftHandTransform1P(FVector& OutPosition, FRotator& OutRotation)
{
	if (EquippedWeapon 
		&& Arms1PMesh 
		&& EquippedWeapon->WeaponAttachment1P 
		&& EquippedWeapon->WeaponAttachment1P->GetRootComponent())
	{
		int32 BoneIndex = Arms1PMesh->GetBoneIndex("b_LeftHand");

		if (BoneIndex != INDEX_NONE)
		{
			FMatrix HandBoneToWorldTM = Arms1PMesh->GetBoneMatrix(BoneIndex);
			
			FMatrix HandSocketWorld = Arms1PMesh->GetSocketTransform(FName("LeftWeapon"), ERelativeTransformSpace::RTS_World).ToMatrixWithScale();
			FMatrix WepSocketWorld = EquippedWeapon->WeaponAttachment1P->GetRootComponent()->GetSocketTransform(FName("LeftHand"), ERelativeTransformSpace::RTS_World).ToMatrixWithScale();
			
			FMatrix HandRelXform = HandSocketWorld * HandBoneToWorldTM.InverseSafe();

			//FMatrix DestXform = FMatrix(FVector(1.f, 0.f, 0.f), FVector(0.f, -1.f, 0.f), FVector(0.f, 0.f, -1.f), FVector(0.f, 0.f, 0.f)) * HandRelXform * WepSocketWorld;
			FMatrix DestXform = HandRelXform * WepSocketWorld;

			OutPosition = DestXform.GetOrigin();
			OutRotation = DestXform.Rotator();
    }
}