I'll take a stab at this since no one else has yet
I don't know how far along you are with this, but here is a rough idea of how I would model my data.
ActionScript Code:
// create a few different types of connections.
// I'd prefer to just create this with a loop since they're all so similar
var connectionA:Object = new Object();
connectionA.type = 0;// id so we can easily recognize possible connection
connectionA.piece_male = "connection_0_male";// linkage to male connection
connectionA.piece_female = "connection_0_female";// linkage to female connection
var connectionB:Object = new Object();
connectionB.type = 1;// id so we can easily recognize possible connection
connectionB.piece_male = "connection_1_male";// linkage to male connection
connectionB.piece_female = "connection_1_female";// linkage to female connection
var connectionC:Object = new Object();
connectionC.type = 2;// id so we can easily recognize possible connection
connectionC.piece_male = "connection_2_male";// linkage to male connection
connectionC.piece_female = "connection_2_female";// linkage to female connection
// here's a couple sample puzzle pieces
var puzzlePiece1:Object = new Object();
puzzlePiece1.connection_up = {connectionType:connectionA, isMale:true};
puzzlePiece1.connection_down = {connectionType:connectionB, isMale:true};
puzzlePiece1.connection_left = {connectionType:connectionC, isMale:true};
puzzlePiece1.connection_right = {connectionType:connectionA, isMale:true};
var puzzlePiece2:Object = new Object();
puzzlePiece2.connection_up = {connectionType:connectionA, isMale:false};
puzzlePiece2.connection_down = {connectionType:connectionB, isMale:true};
puzzlePiece2.connection_left = {connectionType:connectionB, isMale:false};
puzzlePiece2.connection_right = {connectionType:connectionB, isMale:true};
The important thing to consider is that a puzzle piece consists of 4 connections to neighboring pieces - a connection to the up, down, left and right. A connection in itself is an object that keeps a reference of the type of connection it is, and a reference to its male and female components. When I say male and female components, think like stereo plugs or whatever..one thing (male) snaps into another thing (female), like traditional puzzle pieces.
The idea here is that if two puzzle pieces of connections of the same type and one is male and the other female, they may connect, otherwise they cannot.
Sorry if I did a mediocre job explaining that.
Once you get your head around the data structure, assembling a puzzle would be an easy randomization job. You would want to create the fully assembled puzzle first - create a 2d array, pick a random starting position, and run a breadth first search across your 2d array, creating a new puzzle piece for each slot in your matrix.
Essentially, you'd create the first puzzle piece at a random location, assign it random connection types for all four connections it has, then move on to neighboring puzzle pieces. From this point forward, each neighbor will have at least one neighboring connection that you should already know, so you match up the ones you do know, and assign a random connection type for unchecked neighbors.
If I've lost you at this point, I feel like I'd almost need to draw a picture for you but let's see how much of that you got.